39

Consider the following snippet:

struct Base { };
struct Derived : Base { };

void f(Base &) { std::cout << "f(Base&)\n"; }

template <class T = int>
void g() {
    Derived d;
    f(T{} ? d : d); // 1
}

void f(Derived &) { std::cout << "f(Derived&)\n"; }

int main() {
    g();
}

In this case, I reckon that the function call to f at // 1 should be looked up in phase one, since its argument's type is unambigously Derived&, and thus be resolved to f(Base&) which is the only one in scope.

Clang 3.8.0 agrees with me, but GCC 6.1.0 doesn't, and defers the lookup of f until phase two, where f(Derived&) is picked up.

Which compiler is right ?

9
  • 3
    Filed this as a core issue.
    – Columbo
    Jun 1, 2016 at 10:07
  • 3
    @Columbo I tend to think the rules work fine as they are in this case. I wouldn't want name lookup for f to suddenly change and include more candidates if I changed the conditional expression to T{} ? d : e at some point in the future. Especially if e is a reference that is initialized with d, but may change to refer to something else in the future. Also, being able to force dependent name resolution for a function call using something like f((always_void<T>() , arg)) even if arg is non-dependent seems more like a feature than a bug to me.
    – bogdan
    Jun 1, 2016 at 12:50
  • 1
    @bogdan considering that the conditional operator already has its own rules to deduce the type of its result, and that the type of the condition doesn't interfere, I don't see what would be surprising. The first operand could simply be ignored for the sake of determining whether the expression is dependent. On the other hand, the comma operator's result type depends on both operands, therefore such a shortcut is not possible, so your example would still work fine (which I agree is a very useful feature).
    – Quentin
    Jun 1, 2016 at 13:19
  • 2
    @Quentin Ah, so you think the conditional should be singled out because it cannot be overloaded. It's a good point. (I was under a different impression at first, sorry.) One potential problem that I can think of is that making such a conditional expression not type-dependent would make things like noexcept(T{} ? d : d) not value-dependent, which would be incorrect (T's constructor may throw), so some additional special cases would need to be added. Not sure if it's worth it, but now I do think it's worth considering. The more I think about it, the more interesting your question gets!
    – bogdan
    Jun 1, 2016 at 14:44
  • 1
    @bogdan That is not specific to the conditional operator. noexcept(typeid(T{})) isn't value dependent, either.
    – Columbo
    Jun 1, 2016 at 17:45

3 Answers 3

23

Using the latest version of the C++ standard Currently n4582.

In section 14.6 (p10) it says the name is bound at the point of declaration if the name is not dependent on a template parameter. If it depends on a template parameter this is defined in section 14.6.2.

Section 14.6.2.2 goes on to say an expression is type dependent if any subexpression is type dependent.

Now since the call to f() is dependent on its parameter. You look at the parameter type to see if it is depending on the type. The parameter is False<T>::value ? d : d. Here the first conditional is depending on the type T.

Therefore we conclude that the call is bound at the point of instantiation not declaration. And therefore should bind to: void f(Derived &) { std::cout << "f(Derived&)\n"; }

Thus g++ has the more accurate implementation.

14.6 Name resolution [temp.res]

Para 10:

If a name does not depend on a template-parameter (as defined in 14.6.2), a declaration (or set of declarations) for that name shall be in scope at the point where the name appears in the template definition; the name is bound to the declaration (or declarations) found at that point and this binding is not affected by declarations that are visible at the point of instantiation.

14.6.2.2 Type-dependent expressions [temp.dep.expr]

Except as described below, an expression is type-dependent if any subexpression is type-dependent.

3
  • 1
    Well, that would permit a trinary operator overload to be exposed without causing build breaks (as the existence of such an operator would imply that the type of a?b:c could be dependent on the type of a actually, as opposed to just dependent in the standard). Jun 1, 2016 at 17:33
  • @Yakk: The standard does not say the type is dependent on a, that is obviously defined by the trinary operator itself. It says that it is a Type-Dependant expression (which is used only parameter deduction) which defines at what point of compilation names are bound. Jun 1, 2016 at 18:22
  • 1
    Yes. I was just observing that this quirk of formal Type-Dependancy that disagrees with the actual dependancy of the type means that if trinary overloading was ever added to C++, they wouldn't have to add type dependancy here. As in, a reasonably weak argument why this seeming misfeature or error in the standard might not not be completely silly. Jun 1, 2016 at 18:25
7

I think gcc (and visual studio, by the way) are right on this one.

n4582, §14.6.2.2

Except as described below, an expression is type-dependent if any subexpression is type-dependent.

In T{} ? d : d, there are 3 sub expressions:

  • T{}, obviously type dependent
  • d (2 times), not type dependent

Since there is a type dependent sub expression and the ternary operator does not figure in the list of exceptions in §14.6.2.2, it is considered type dependent.

1
  • 4
    There's not much point looking at MSVC when dealing with two-phase lookup - it doesn't implement it at all.
    – T.C.
    Jun 1, 2016 at 17:33
1

According to c++ draft (n4582) §14.7.1.5:

Unless a function template specialization has been explicitly instantiated or explicitly specialized, the function template specialization is implicitly instantiated when the specialization is referenced in a context that requires a function definition to exist. Unless a call is to a function template explicit specialization or to a member function of an explicitly specialized class template, a default argument for a function template or a member function of a class template is implicitly instantiated when the function is called in a context that requires the value of the default argument.

I would say gcc is more correct about this.

If you for example create an specialized version of void g() you get both compiler doing the same.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.