为什么C++不允许两个同名的函数/类模板,区别仅在于非类型模板参数(整型)的类型?
Why doesn't C++ allow two function/class templates with the same name, differing only by the type of a non-type template parameter (of integral type)?
当我尝试此操作时,编译器给出了错误。我尝试了VC++和g++。
这同样适用于函数模板和类模板(尽管对于函数模板,编译器错误仅在实例化函数模板时发生;当编译器遇到第二个类定义时,类模板的编译器错误会立即发生(。
下面是函数模板的示例:
template <unsigned int>
void Foo() {}
template <signed int> // Same name, only difference is the type of the
void Foo() {} // non-type template parameter (of integral type)
Foo<10U>(); // COMPILER ERROR.
上面,为什么编译器不能只实例化Foo<unsigned int>()
?
我发现如果模板函数/类的第二个版本具有类型模板参数,这不是问题。如果模板函数/类的第二个版本具有非整型的非类型模板参数,这也不是问题:
template <unsigned int>
void Foo() {}
template <unsigned int*> // Non-type template parameter
void Foo() {} // of non-integral type
template <typename T> // Type template parameter
void Foo() {}
Foo<10U>(); // OK
因此,如果我不得不猜测,我会说这与编译器可以在整型值之间进行转换这一事实有关?但这并不能阻止C++允许两个重载函数,这两个函数仅在整数参数的类型上有所不同。
它确实允许它,你能够很好地编写两个模板。它们只是在实例化时变得不可用。原因是没有"重载解决方案"来选择模板。对非类型模板参数施加的唯一要求如下:
[temp.arg.nontype]/2
非类型模板参数的模板参数应为 转换后的模板参数类型的常量表达式。
10u
是int
和unsigned int
类型的转换常量表达式。有效作为任一重载的参数。
相关文章:
- 基本类型与内置类型有什么区别C++
- C++指针中的这两种类型的值分配有什么区别?
- 为什么C++不允许两个同名的函数/类模板,区别仅在于非类型模板参数(整型)的类型?
- 无符号长整型和无符号 int 之间有什么区别,这 2 种类型应该如何在 c# 中封送?
- CRTP:为什么获得嵌套类型和派生类的嵌套方法有区别
- 类型测试对象的动态数组的这两个声明之间的区别?
- C++ 中的对象创建类型有什么区别?
- C++11中的自动类型扣除和类型转换有什么区别
- a、&a 和 &a[0] 之间的类型有什么区别?
- 将类类型成员定义为公共和私有之间有什么区别?
- 两种类型的定义构造函数有什么区别?第一个使用":",第二个用大括号(如函数)定义它
- 类型、value_type和element_type之间有什么区别,以及何时使用它们?
- 自动扣款和模板类型扣款有什么区别
- 抽象数据类型(ADT)和数据结构之间有什么区别
- C 中类型和类之间的区别
- 使用函数类型和函数指针作为C++模板函数参数的区别
- 整型非类型参数和非整型非类型参数的模板部分特化,g++和clang的区别
- VS c++和MinGW实现双重类型有什么区别
- 类型转换时C++ (void *) 变量和 (void *&) 变量有什么区别
- C++中的类型和名称有什么区别?