Home > Backend Development > C++ > body text

C++ syntax error: non-template types cannot be used in template parameters, how to solve it?

王林
Release: 2023-08-22 11:37:50
Original
1145 people have browsed it

C++ syntax error: non-template types cannot be used in template parameters, how to solve it?

In C template programming, we can use a non-template class as a template parameter, but there are certain restrictions on this use. When trying to use a non-template type as a template parameter, you may encounter the error "Non-template types cannot be used in template parameters." This error usually occurs because the C compiler cannot determine how to handle the non-template type.

In this article, we will discuss the causes of this error and how to fix it. We will first explore the causes of this error and then discuss ways to resolve it.

Error reason

When we write the following code:

template <class T, int N>
class MyClass {
    T array[N];
    // some code here
};

MyClass<int, 10> myClass;
Copy after login

MyClass in the code is a template class, which has two templates Parameters T and N. T is a data type, N is an integer. After processing by the compiler, myClass should be an instance of the MyClass class, where the T type is int, N The value is 10.

The problem is that when we try to use a non-template type as a template parameter, an error will occur, for example:

template <class T, int N, double D>
class MyClass2 {
    // some code here
};

MyClass2<int, 10, 3.14> myClass2;
Copy after login

This code will double type The value 3.14 is used as the third template parameter of the MyClass2 class. This is wrong because the double type is not a template parameter and cannot be used in template parameters.

However, sometimes we may try to use a non-template class as a template parameter. At this time, we may encounter the error "Non-template types cannot be used in template parameters". For example:

class MyClass3 {
    // some code here
};

template <class T, MyClass3 myClass3>
class MyClass4 {
    // some code here
};

MyClass4<int, MyClass3> myClass4;
Copy after login

This code defines a template class named MyClass4. The second template parameter is an instance named myClass3. Is an object of class MyClass3. When defining myClass4, we use MyClass3 as the second template parameter of MyClass4. However, this is wrong because MyClass3 is not a template parameter and cannot be used in template parameters.

Solution

There is a way to solve this error, which is to change the non-template class to a template class.

template <typename T>
class MyClass5 {
    // some code here
};

template <class T, template <typename> class N>
class MyClass6 {
    N<T> myClass;
    // some code here
};

MyClass6<int, MyClass5> myClass6;
Copy after login

In this example, the second template parameter of the MyClass6 class has become the template class. In this way, we can use a template class as a template parameter. When we define myClass6, we can use MyClass5 as the second template parameter of MyClass6.

Using this method can effectively solve the error "non-template types cannot be used in template parameters". This way we can change non-template types to template types so that they can be used in template parameters.

Conclusion

In the process of template programming in C, sometimes we may try to use a non-template class as a template parameter, but there are limitations to this use. We cannot directly use a non-template class as a template parameter because the compiler cannot determine how to handle this non-template type. But by changing the non-template type to a template type, we can solve this problem.

The above is the detailed content of C++ syntax error: non-template types cannot be used in template parameters, how to solve it?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template