模板专业化不明确
Ambiguous template specialization
我有一个带有paint()
模板函数的Painter
模板类。我正在使用标记来专门化模板类中的模板函数。我在Painter.h
中放置了模板函数paint()
的定义,在Painter.cpp
中放置了重载函数的定义。
当我在Painter.cpp
中显式实例化paint()
时,我遇到编译器错误我的要求是我需要在Painter.cpp文件中实现重载函数paint(ColorTag<Color::RED>)
。
源文件如下:
油漆工.h
#include <iostream>
enum class Color {
RED = 0,
GREEN = 1,
BLUE = 2
};
template<Color>
struct ColorTag {
};
template<typename T>
class Painter {
public:
template<Color MyColor>
void paint(ColorTag<MyColor>);
void paint(ColorTag<Color::RED>);
};
template<typename T>
template<Color MyColor>
void Painter<T>::paint(ColorTag<MyColor>){
std::cout << "General" << std::endl;
}
画家.cpp
#include "Painter.h"
template<typename T>
void Painter<T>::paint(ColorTag<Color::RED>){
std::cout << "RED" << std::endl;
}
template void Painter<int>::paint(ColorTag<Color::RED>);
Main.cpp
#include "Painter.h"
int main(){
Painter<int> painter;
painter.paint(ColorTag<Color::RED>());
return 0;
}
使用编译
g++ Main.cpp Painter.cpp -std=c++11
当我在Painter.cpp 中显式实例化paint()
时,我得到了以下编译器错误
Painter.cpp:8:15: error: ambiguous template specialization ‘paint<>’ for ‘void Painter<int>::paint(ColorTag<(Color)0>)’
template void Painter<int>::paint(ColorTag<Color::RED>);
^
Painter.cpp:4:6: note: candidates are: void Painter<T>::paint(ColorTag<(Color)0>) [with T = int]
void Painter<T>::paint(ColorTag<Color::RED>){
^
In file included from Painter.cpp:1:0:
Painter.h:20:10: note: template<Color MyColor> void Painter<T>::paint(ColorTag<MyColor>) [with Color MyColor = MyColor; T = int]
void paint(ColorTag<MyColor>);
我尝试过的
首先我创建了一个名为instantiatePaint()
的模板函数来调用paint()
函数。然后我将它放置并实例化在Painter.cpp
文件中。这起到了作用。但这种感觉有点尴尬。
template<typename T>
template<Color MyColor>
void Painter<T>::instantiatePaint(ColorTag<MyColor>){
paint(ColorTag<MyColor>());
}
template void Painter<int>::instantiatePaint(ColorTag<Color::RED>);
其次将重载函数定义从Painter.cpp
移动到Painter.h
。这起作用,但打破了我在Painter.cpp
中重载函数paint(ColorTag<Color::RED>)
的要求。
有没有更好的方法来解决这个问题,或者到底是什么导致了歧义?
您想要的是T = int
的显式专用化,因此正确的语法是:
template<>
void Painter<int>::paint(ColorTag<Color::RED>);
使用这种语法,g++和clang都可以编译代码。实例
为什么不能初始化整个类而不是函数?
template class Painter<int>;
#include <iostream>
enum Color {
RED = 0,
GREEN = 1,
BLUE = 2
};
template<Color color = Color::RED>
struct ColorTag
{
// your Implementation
};
template<>
struct ColorTag <Color::RED>
{
// your Implementation specific to Color::RED
};
template<typename T>
class Painter {
public:
// template specialization can not apply on method level
// use class or struct
template<Color MyColor>
void paint(ColorTag<MyColor>);
};
template<typename T>
template<Color MyColor>
void Painter<T>::paint(ColorTag<MyColor> colorTag)
{
std::cout << "General"<<std::endl;
}
在visual Studio 2012上运行良好不确定Gcc
相关文章:
- Visual C++(VS2017)中用户定义的转换不明确
- 重载类方法的不明确调用
- 为函数定义符号不明确的指针参数
- 父类的私有函数会导致对具有相同名称和相似参数的子类中的公共函数的不明确调用
- 在 C++17 中的命名空间和子命名空间中重载运算符是不明确的
- C++ 编译器错误:P1LinkedList.cpp:145:错误:重载的"to_string(int&)"调用不明确
- 对重载函数find_first_not_of的不明确调用
- 不明确的成员模板查找
- gcc出现不明确的模板实例化错误
- 调用'Node'构造函数是不明确的
- 如何解决不明确的运算符过载问题?
- 使用 nullptr 调用重载方法是不明确的
- "fpclassify":对重载函数的不明确调用
- 对"列表"的引用不明确,包括头文件
- 删除全局隐式函数 - 避免使用不明确的运算符
- 当接收到不明确的规范时,表示图的邻接列表的数据结构
- "byte":使用Crypto++和Windows SDK时出现不明确的符号错误
- 使用 std::iterator_traits<> 时编译时错误不明确
- 模板专业化不明确
- 模板专业化是不明确的