代码之家  ›  专栏  ›  技术社区  ›  BCS

通过使用别名显式专门化类模板是否有效?

  •  2
  • BCS  · 技术社区  · 5 月前

    首先,我在做什么 似乎 致力于所有 GCC/Clang/MSVC/icc/etc ,所以我的问题是; 这是偶然的,还是标准要求允许的?

    从某种意义上说,它似乎是东西的逻辑扩展,但另一方面,语法看起来很奇怪,我担心自己会陷入沉思 depending-on-bugs 领土:

    #include <type_traits>
    
    template <int, bool>
    void Bar();
    
    template <int i>
    struct A {
        struct T : std::true_type {};
        static void Foo() { Bar<i, T::value>(); }
    };
    
    // The normal way to do thing:
    template <> struct A<1>::T : std::false_type {};
    
    // But this also seems to work despite looking very odd:
    using A3 = A<3>;
    template <> struct A3::T : std::false_type {};
    
    void Go() {
        A<0>::Foo();  // Calls Bar<0, true>()
        A<1>::Foo();  // Calls Bar<1, false>()
        A<2>::Foo();  // Calls Bar<2, true>()
        A<3>::Foo();  // Calls Bar<3, false>()
    }
    
    1 回复  |  直到 5 月前
        1
  •  4
  •   user17732522    5 月前

    它是不成形的。

    请参阅 [temp.spec.general]/3 :

    在[…]类模板[…]的成员的显式特化声明中,显式特化的变量或类应使用 简单模板id .

    A3 不是a 简单模板id .