【发布时间】:2016-11-08 23:22:54
【问题描述】:
我有以下示例代码,想获得一些帮助以了解为什么我无法在 Linux 上使用 clang 和 g++ 编译它?
#include <iostream>
using namespace std;
typedef enum COLORS {
RED = 0,
GREEN,
BLUE,
ORANGE,
MAROON,
WHITE,
BLACK
} COLORS;
template <COLORS C> void whatColor( COLORS x ) {
cout << "this can be any color!!!" << endl;
}
template<> void whatColor<RED>( COLORS x ) {
cout << "this is RED!!!" << endl;
}
template<> void whatColor<GREEN>( COLORS x ) {
cout << "this is GREEN!!!" << endl;
}
template<> void whatColor<BLUE>( COLORS x ) {
cout << "this is BLUE!!!" << endl;
}
template<> void whatColor<ORANGE>( COLORS x ) {
cout << "this is ORANGE!!!" << endl;
}
int main( ) {
const COLORS red=RED;
whatColor( red );
whatColor<RED>( RED );
whatColor<red>( red );
whatColor<RED>( red );
}
我看到的失败是这样的:
CXX [misc] tmpl.cpp
src/tmpl.cpp:40:2: error: no matching function for call to 'whatColor'
whatColor( red );
^~~~~~~~~
src/tmpl.cpp:15:26: note: candidate template ignored: couldn't infer template argument 'C'
template <COLORS C> void whatColor( COLORS x ) {
^
1 error generated.
make: *** [obj/tmpl.o] Error 1
我不清楚为什么在这种情况下它不能推断出参数类型
【问题讨论】:
标签: templates g++ clang template-specialization clang++