Home Backend Development C++ C++ compilation error: local types are not allowed as template parameters, how to deal with it?

C++ compilation error: local types are not allowed as template parameters, how to deal with it?

Aug 21, 2023 pm 09:39 PM
c++ compilation error template parameters local type

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;
}
Copy after login

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;
}
Copy after login

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;
}
Copy after login

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to solve C++ compilation error: 'no matching function for call to 'function'? How to solve C++ compilation error: 'no matching function for call to 'function'? Aug 25, 2023 pm 04:31 PM

Solve C++ compilation error: 'nomatchingfunctionforcallto'function'', how to solve it? When writing programs in C++, we often encounter various compilation errors. One of the common errors is "nomatchingfunctionforcallto'function'". This error usually occurs when a function is called and the compiler cannot find a matching function declaration or definition. Book

Solve C++ compilation error: 'incompatible types', how to solve it? Solve C++ compilation error: 'incompatible types', how to solve it? Aug 25, 2023 pm 05:13 PM

Solve C++ compilation error: 'incompatibletypes', how to solve it? During the development process of C++, we often encounter error messages given by the compiler. One common type of error is "incompatibletypes". This error message indicates that there is a type mismatch in the program, which may be inconsistent variable types, mismatched function parameter types, etc. This article will introduce several common type incompatibility errors and give corresponding solutions.

C++ compilation error: Duplicate definition of function parameters, how to solve it? C++ compilation error: Duplicate definition of function parameters, how to solve it? Aug 22, 2023 pm 12:33 PM

As an efficient programming language, C++ is widely used in various fields because of its reliability. However, in the process of writing code, we often encounter some compilation errors, and repeated definition of function parameters is one of them. This article will detail the reasons and solutions for repeatedly defining function parameters. What is repeatedly defining function parameters? In C++ programming, function parameters refer to variables or expressions that appear in function definitions and declarations and are used to accept actual parameters passed when a function is called. When defining a function's argument list, each argument must be

How to solve C++ compilation error: 'ambiguous overload for 'function'? How to solve C++ compilation error: 'ambiguous overload for 'function'? Aug 26, 2023 pm 12:30 PM

How to solve C++ compilation error: 'ambiguousoverloadfor'function''? When programming in C++, we often encounter compilation errors. Among them, a common error is 'ambiguousoverloadfor'function'. This error reminds us that there is ambiguity in overloading functions when calling functions. This article will explain the causes of this error and provide several solutions to resolve it. First, let

How to solve C++ compilation error: 'redefinition of 'function'? How to solve C++ compilation error: 'redefinition of 'function'? Aug 27, 2023 pm 02:27 PM

Solve C++ compilation error: 'redefinitionof'function'', how to solve it? As a powerful programming language, C++ is often widely used in software development. However, writing error-free C++ programs is not easy for beginners. One of the common errors is "redefinitionof'function'", which is a function redefinition error. In this article I will explain the causes of this error and how to fix it. wrong reason

C++ compilation error: multiple definitions, how to modify them? C++ compilation error: multiple definitions, how to modify them? Aug 21, 2023 pm 11:07 PM

In C++ programming, "multiple definition" (multiple definitions) compilation errors often occur. This is because multiple variables, functions, or objects with the same name are defined in the program. These variables, functions, or objects are all considered to be the same by the compiler, so the compiler will generate a "multipledefinition" error. In actual programming, how should we avoid and solve such problems? Using header files in C++, we can convert some reused functions or variables into

How to solve the C++ compilation error: 'invalid initialization of reference of type 'type&' from expression of type 'type''? How to solve the C++ compilation error: 'invalid initialization of reference of type 'type&' from expression of type 'type''? Aug 25, 2023 pm 11:43 PM

How to solve the C++ compilation error: 'invalidinitializationofreferenceoftype'type&'fromexpressionoftype'type''? Problem background: In C++ programming, we sometimes encounter compilation errors. One of them is the error message "invalidinitializationofreferenceof

C++ compilation error: Undefined variable used, how to solve it? C++ compilation error: Undefined variable used, how to solve it? Aug 22, 2023 pm 03:01 PM

C++ compilation error: Undefined variable used, how to solve it? When writing C++ programs, we often encounter compilation errors, one of the more common errors is the use of undefined variables. If you encounter this error, don't worry, next, this article will introduce you how to solve this error. The reason for this error is that an undefined or undeclared variable is used in the program. The C++ compiler does not find the definition of this variable, so it cannot allocate memory space, causing the compiler to generate an error. The solution to this problem is as follows

See all articles