0

Why a fully specialized class template can't be defined in a non-fully specialized one?

template<typename TW>
struct Wrapper {
    template<typename T>
    struct Fun_ {
        constexpr static int vlaue = 0;
    };
    template<> // error
    struct Fun_<int> { // error
        constexpr static int value = 1;
    };
};

int main() {
}

GCC version:

gcc (Ubuntu 5.4.0-6ubuntu1~16.04.11) 5.4.0 20160609

GCC error:

test.cpp:7:14: error: explicit specialization in non-namespace scope ‘struct Wrapper<TW>’
     template<>
              ^
test.cpp:8:12: error: template parameters not deducible in partial specialization:
     struct Fun_<int> {
            ^

Clang version:

clang version 6.0.0-1ubuntu2~16.04.1 (tags/RELEASE_600/final)
Target: x86_64-pc-linux-gnu

Clang error:

test.cpp:8:12: error: explicit specialization of 'Fun_' in class
      scope
    struct Fun_<int> {
           ^

However, by converting the fully specialized class template into a partially specialized one using an additional pseudo template argument, the following can compile:

template<typename TW>
struct Wrapper {
    // TDummy is a pseudo template argument with default value void
    template<typename T, typename TDummy = void>
    struct Fun_ {
        constexpr static int value = 0;
    };
    template<typename TDummy>
    struct Fun_<int, TDummy> { // partial specialization
        constexpr static int value = 1;
    };
};

int main() {
}
chaosink
  • 1,329
  • 13
  • 27
  • 1
    What compiler is being used? VC++ is (or at least used to be) non-compliant in that regard and required explicit specializations to be at namespace level; see https://stackoverflow.com/questions/3052579/explicit-specialization-in-non-namespace-scope – IGarFieldI Jan 15 '19 at 00:09
  • I confirm your problem (in first example) with g++ (wandbox) but clang++ compile without problem. A bug? – max66 Jan 15 '19 at 02:23
  • 2
    Possible duplicate of [Explicit specialization in non-namespace scope does not compile in GCC](https://stackoverflow.com/questions/49707184/explicit-specialization-in-non-namespace-scope-does-not-compile-in-gcc) – Jans Jan 15 '19 at 03:25
  • @max66 - clang is right; in c++17 this is possible. See the duplicate link. – Jans Jan 15 '19 at 03:27
  • @max66 However my Clang get the error, even in C++17? What's the version do you use? – chaosink Feb 27 '19 at 03:26
  • @Jans However my Clang get the error, even in C++17? What's the version do you use? – chaosink Feb 27 '19 at 03:26

0 Answers0