variadic templates - ambiguous call

后端 未结 2 1647
有刺的猬
有刺的猬 2021-01-04 14:15

The following code compiles in both gcc 4.7.2 and MSVC-11.0:

template 
void foo(T bar) {}

template 
vo         


        
相关标签:
2条回答
  • 2021-01-04 14:43

    This is considered a defect in the current standard. Even the standard itself relies on non-variadic templates to be partially ordered before variadic ones in the specification of std::common_type:

    §20.9.7.6 [meta.trans.other] p3

    The nested typedef common_type::type shall be defined as follows:

    template <class ...T> struct common_type;
    
    template <class T>
    struct common_type<T> {
      typedef T type;
    };
    
    template <class T, class U>
    struct common_type<T, U> {
      typedef decltype(true ? declval<T>() : declval<U>()) type;
    };
    
    template <class T, class U, class... V>
    struct common_type<T, U, V...> {
      typedef typename common_type<typename common_type<T, U>::type, V...>::type type;
    };
    

    Specifically common_type<T, U> vs common_type<T, U, V...>.

    0 讨论(0)
  • 2021-01-04 14:56

    Yep, you're right! That's a compiler "feature", and quite possibly a deliberate one since the committee has suggested, in issue #1395, that this case should be accepted and, as such, it seems likely that in future standards (or even a TR) it will be.

    0 讨论(0)
提交回复
热议问题