简体   繁体   English

当我们声明一个额外的模板参数而不在定义中使用时,为什么编译器会抛出错误?

[英]Why does compiler throw an error when we declare an extra template argument and not used in definition?

I have a below code. 我有下面的代码。

#include <iostream>

template <class T,class U>
T myMax(T x, T y)
{
   return (x > y)? x: y;
}

int main()
{
  std::cout << myMax(3, 7) << std::endl;  // Call myMax for int
  std::cout << myMax(3.0, 7.0) << std::endl; // call myMax for double
  std::cout << myMax('g', 'e') << std::endl;   // call myMax for char
  return 0;
}

On compiling the code, compiler reports an error as show below. 编译代码时,编译器将报告错误,如下所示。

functionTemplates.cpp: In function ‘int main()’:
functionTemplates.cpp:18: error: no matching function for call to ‘myMax(int, int)’
functionTemplates.cpp:19: error: no matching function for call to ‘myMax(double, double)’
functionTemplates.cpp:20: error: no matching function for call to ‘myMax(char, char)’

I know that if i remove class U, compilation will be successful. 我知道,如果我删除U类,编译将成功。 But i want to know why does compiler bothers about an unused parameter? 但是我想知道为什么编译器会为一个未使用的参数烦恼?

The compiler cannot determine the type of the unused template argument. 编译器无法确定未使用的模板参数的类型。 You need to specify it explicitly, or remove the unused template argument. 您需要明确指定它,或删除未使用的模板参数。

For a general case, a compiler can determine the template parameters from: 对于一般情况,编译器可以从以下命令确定模板参数:

  1. The arguments used to make the function call. 用于进行函数调用的参数。
  2. The explicitly used types to make the function call. 显式使用的类型进行函数调用。

In your case, U cannot be determined from the arguments used to make the function call since U is not used by the arguments. 在你的情况, U不能从用来做函数调用因为参数确定U不使用的参数。 The only other way the compiler can determine U is if it is used explicitly in the function call. 编译器确定U的唯一其他方法是,是否在函数调用中显式使用它。 Eg 例如

 std::cout << myMax<int, double>(3, 7) << std::endl;

PS It's not clear to me why you have U as a template parameter in the first place. PS对我来说不清楚,为什么首先要使用U作为模板参数。 It's not used at all. 根本不使用。 Won't it be easier to use: 使用起来会不会更容易:

template <class T>
T myMax(T x, T y)
{
   return (x > y)? x: y;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C ++:为什么编译器在不使用模板时将其实例化 - C++: Why does the compiler instantiate a template when it is not used 为什么当我声明并定义不带参数的构造函数和带参数的构造函数时,编译器会抱怨? - Why does the compiler complains when I declare and define a constructor with no argument and a constructor with an argument? 为什么编译器不抛出编译错误? - Why does compiler not throw compilation error? 当typedef const指针与额外的const一起使用时,为什么编译器不会出错? - Why compiler doesn't give error when typedef const pointer is used with extra const? 使用不受支持的类方法时引发编译器错误 - Throw a compiler error when unsupported class method is used 为什么编译器在提供无参数函数模板时尝试推断? - Why does the compiler try to deduce when an argument-less function template is provided? 为什么编译器会发出模板递归错误? - Why does the compiler issue a template recursion error? 为什么在这种情况下编译器会抛出“undefined reference to...”错误? - Why does the compiler throw “undefined reference to…” error in this case? 为什么我的编译器会抱怨一个概念缺少模板参数? - Why does my compiler complain about a missing template argument for a concept? 当定义默认值时,为什么boost :: program_options会引发有关必需参数的错误 - Why does boost::program_options throw an error about a required argument when the default is defined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM