When writing C code, sometimes you will encounter a compilation error such as "local types are not allowed as template parameters". This usually means that we are using a local type in a template parameter, such as a class or struct type defined inside a function. In this article, we will discuss this problem and how to solve it.
First, let's take a look at why this compilation error occurs. In C, template parameters must be resolved at compile time, while local type definition occurs at runtime. Therefore, local types cannot be used as template parameters because the compiler does not know how to parse them.
Give an example to illustrate this problem:
#include <iostream> template <typename T> void printSize(const T& arg){ struct localStruct { int i; }myLocalStruct; //定义了一个局部结构体类型 std::cout << "Size of arg = "<<sizeof(arg)<<" "; std::cout << "Size of localStruct = "<<sizeof(myLocalStruct)<<" "; } int main() { int x = 5; printSize(x); return 0; }
In the above code, we define a template function printSize, which receives a parameter arg. We also define a local structure type myLocalStruct and use sizeof to get the size of it and the parameter arg.
When we compile this code, we get an error message: "Local types are not allowed as template parameters".
To solve this problem, we need to convert the local type to a global type. We can move the local type definition outside the function, or define it as a member type of the class.
Let's see how we can fix the above code using global types:
#include <iostream> struct localStruct { int i; }; //将局部结构体类型定义为全局 template <typename T> void printSize(const T& arg){ localStruct myLocalStruct; std::cout << "Size of arg = "<<sizeof(arg)<<" "; std::cout << "Size of localStruct = "<<sizeof(myLocalStruct)<<" "; } int main() { int x = 5; printSize(x); return 0; }
Now, we have moved the local structure definition outside the function. This fix compiles and runs successfully, and the output is correct.
In addition to converting local types to global types, another solution is to define the local type as a member type of the class. This method requires some extra code, but is sometimes more convenient:
#include <iostream> template <typename T> class myClass{ public: struct localStruct { int i; }; void printSize(const T& arg){ localStruct myLocalStruct; std::cout << "Size of arg = "<<sizeof(arg)<<" "; std::cout << "Size of localStruct = "<<sizeof(myLocalStruct)<<" "; } }; int main() { int x = 5; myClass<int> obj; obj.printSize(x); return 0; }
In the above code, we define the local structure type as the member type of myClass. This fix also compiles and runs successfully and outputs the correct results.
To summarize, when we encounter a compilation error of "local types are not allowed as template parameters" when using C templates, we need to convert the local type to a global type or a member type of the class. These fixes can successfully resolve this issue.
The above is the detailed content of C++ compilation error: local types are not allowed as template parameters, how to deal with it?. For more information, please follow other related articles on the PHP Chinese website!