why no need of forward declaration in static dispatching via templates?

后端 未结 3 693
迷失自我
迷失自我 2021-02-13 04:44

I am playing a bit with static polymorphism, I\'m calling a function which internally calls the \"right\" specialized function depending on the type of the initial argument (bas

3条回答
  •  悲哀的现实
    2021-02-13 05:32

    I agree, the code is ill-formed. I'm surprised neither g++ nor clang++ has even a warning about this.

    14.6.2/1:

    In an expression of the form:

    • postfix-expression ( expression-list [opt] )

    where the postfix-expression is an id-expression, the id-expression denotes a dependent name if any of the expressions in the expression-list is a type-dependent expression (14.6.2.2) or if the unqualified-id of the id-expression is a template-id in which any of the template arguments depends on a template parameter. ... Such names are unbound and are looked up at the point of the template instantiation (14.6.4.1) in both the context of the template definition and the context of the point of instantiation.

    [f_helper is a postfix-expression and id-expression, and typename T::tag_type{} is type-dependent, so f_helper is a dependent name.]

    14.6.4/1:

    In resolving dependent names, names from the following sources are considered:

    • Declarations that are visible at the point of definition of the template.

    • Declarations from namespaces associated with the types of the function arguments both from the instantiation context (14.6.4.1) and from the definition context.

    14.6.4.1/6:

    The instantiation context of an expression that depends on the template arguments is the set of declarations with external linkage declared prior to the point of instantiation of the template specialization in the same translation unit.

    14.6.4.2/1:

    For a function call that depends on a template parameter, the candidate functions are found using the usual lookup rules (3.4.1, 3.4.2, 3.4.3) except that:

    • For the part of the lookup using unqualified name lookup (3.4.1) or qualified name lookup (3.4.3), only function declarations from the template definition context are found.

    • For the part of the lookup using associated namespaces (3.4.2), only function declarations found in either the template definition context or the template instantiation context are found.

提交回复
热议问题