简体   繁体   中英

Partially specializing on non-type template parameter of the wrong type

Consider the following:

template <unsigned >
struct uint_ { };

template <class >
struct X {
    static constexpr bool value = false;
};

template <int I> // NB: int, not unsigned
struct X<uint_<I>> {
    static constexpr bool value = true;
};

int main() {
    static_assert(X<uint_<0>>::value, "!");
}

clang compiles the code, gcc does not.

However, in the following highly related example:

template <unsigned >
struct uint_ { };

template <int I> // NB: int, not unsigned
void foo(uint_<I> ) { }

int main() {
    foo(uint_<0>{} );
}

both compilers reject with no matching function call to foo . gcc's behavior is consistent, clang's is not - so one or the other compiler has a bug for one or both examples. Which compiler is correct?

GCC is correct. [temp.deduct.type]/17 :

If P has a form that contains <i> , and if the type of the corresponding value of A differs from the type of i , deduction fails.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM