9

以下代码使用clang++ 6.0.0g++ 7.3.0编译良好(编译标志为-std=c++14 -Wall -Wextra -Werror -pedantic-errors)但无法使用vc++ 19.10.25017编译(编译标志为/Za

template <typename>
struct A
{
    template <typename>
    struct B
    {
    };
};

template <>
template <>
struct A<int>::B<char>
{
    static void foo();
};

void A<int>::B<char>::foo()
{
}

int main()
{
}

vc++编译错误信息:

错误 C2906:“void A<int>::B<char>::foo(void)”:显式特化需要“模板 <>”

在这种情况下,什么行为符合标准?

4

1 回答 1

4

VC++ 是错误的。可能误解以下条款

显式特化类模板的成员以与普通类成员相同的方式定义,并且不使用template<> 语法。定义显式专用成员类 (*) 的成员时也是如此。但是,template<>用于定义明确专门化的成员类模板的成员,该类模板专门化为类模板。

后一条规则的目的是消除歧义:

// Which template does this header appertain to?
template<class U> void A<short>::C<U>::f() { /* ... */ } 

但是,在您的情况下, (*) 情况适用。

于 2018-04-24T18:19:50.993 回答