Using partial specialization in C ++ 11

I have the following code:

template<class T, int I=44> struct test {T j = I;};

template<int J> struct test<int, J> {int j = J;};

int main()
{
  test<55> jj;

  std::cout << jj.j << std::endl;
  return(1);
}

The compiler (clang) only complains about the string test<55> jj

I do not understand why? Is there any work?

And if he complains about this line, why doesn't she complain about the second definition of the template?

Thanks in advance.

Message:

enable.cpp:17:8: error: template argument for template type parameter must be a type
test<55> jj;
   ^~
enable.cpp:9:16: note: template parameter is declared here
template<class T, int I=44> struct test
+5
source share
2 answers

The problem is that you did not understand how the specialized specialization of class templates works.

Your specialization:

template<int J> struct test<int, J> {int j = J;};

does not create a template for which you need to pass only one int template parameter.

test<55> jj; // doesn't work because there no template<int J> struct test

Instead, it creates a specialization template<class T, int I> struct testthat will be used when the template arguments in template<class T, int I> struct testcorrespond to the specialization, i.e. test<int,J>.

test<int,55> jj; // uses the specialization template<int J> struct test<int, J>

Here's a key quote from the standard:

, (, A<int, int, 1>) . . [ ]

                                                                                    - nbsp;


, int T, I. , , , 44 int .

, , . (template<class T=int, int I=44> struct test), .

, , :

template <int I>
using test_int = test<int, I>;

, :

test_int<55> jj;

, test<int, I> , .

+6

- , (class T) 55. , 55 , . , test<int> , parater , .

"currying" , . :

template <typename T>
struct test2 = using test<T, 55>;

T, 55 . , T, , test2<double>.

, :

template <int I>
using test3 = test<int, I>;

int, , :

test3<55> t;
+3

All Articles