It seems that the rules for the template instance in Clang (3.8) and GNU C ++ (4.9) do not match. Here is an example:
#include <cstddef> template <bool> class Assert { Assert(); // private constructor for Assert<false> }; template <> class Assert<true> { // implicit public constructor for Assert<true> }; template <size_t N> class A { }; template <class T, size_t N> T foo(A<N>) { return T(N - 1); } template <class T> T foo(A<0>) { // foo is not defined for N=0 Assert<false>(); return T(0); } int main(int argc, char **argv) { foo<int>(A<3>()); return 0; }
This minimal example shows a template function foo that generalizes to type T and a positive integer N This function is not defined for N=0 , so I would like to use the Assert class to signal a compiler error if it is used this way.
This code is accepted by the GNU compiler (as well as Visual C ++ 2015), but Clang gives an error for "invoking the private constructor of the Assert<false> class."
So who is right? As I see, there is no call to foo<T,0> , so there is no need to instantiate this template ...
EDIT: Accepting an interpretation of the standard Clang standard, what is the canonical way to force control over template parameters at compile time?
c ++ gcc instantiation templates clang ++
vukung
source share