Conditional operator's return type and two-phase lookup

后端 未结 3 1332
悲&欢浪女
悲&欢浪女 2021-02-06 21:42

Consider the following snippet:

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

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

template 

        
3条回答
  •  无人共我
    2021-02-06 22:16

    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::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.

提交回复
热议问题