C++ 什么是c++;使用pass enum param作为模板参数实现嵌套类函数的语法是否正确?
嗯。此处代码:C++ 什么是c++;使用pass enum param作为模板参数实现嵌套类函数的语法是否正确?,c++,templates,enums,g++,clang,C++,Templates,Enums,G++,Clang,嗯。此处代码: #include <iostream> using namespace std; template<typename T> struct A { enum Status { one = 1 }; template< Status status > struct C; }; template<typename T> template<typename A<T&
#include <iostream>
using namespace std;
template<typename T>
struct A {
enum Status {
one = 1
};
template< Status status >
struct C;
};
template<typename T>
template<typename A<T>::Status status >
struct A<T>::C {
void operator()() {
cout << "C: " << (int)status << endl;
};
// void operator()();
};
// template<typename T>
// template<typename A<T>::Status status >
// void A<T>::C<status>::operator()() {
// cout << "C: " << status << endl;
//}
int main()
{
A<int>::C<A<int>::one> c;
c();
return 0;
}
我尝试在工作PC上用Visual C++ 2010编译代码,但两个样本都失败了。我现在无法验证它(没有windows PC),我也不确定-也许我犯了其他错误
第二个示例中的错误在哪里?我们实现的正确语法是什么?在我看来,您只是忘记了struct C实现中的模板参数。 下面的固定代码在Visual C++ 2010中编译文件:
#include <iostream>
#include <string>
#include <sstream>
#include <iostream>
using namespace std;
template<typename T>
struct A {
enum Status {
one = 1
};
template< Status status >
struct C;
};
template<typename T>
template<typename A<T>::Status status >
struct A<T>::C<status> {
/*void operator()() {
cout << "C: " << (int)status << endl;
};*/
void operator()();
};
template<typename T>
template<typename A<T>::Status status >
void A<T>::C<status>::operator()() {
cout << "C: " << status << endl;
}
int main()
{
A<int>::C<A<int>::one> c;
c();
return 0;
}
#包括
#包括
#包括
#包括
使用名称空间std;
模板
结构A{
枚举状态{
一=1
};
模板<状态>
结构C;
};
模板
模板
结构A::C{
/*void运算符()(){
我不知道为什么投票决定关闭它,因为我不清楚。既不是clang++也不是VC(没有震惊)这个问题只是想知道,当g++消费时,它的定义叮当作响和VC吐出来是因为它被破坏了还是因为g++太放松了。如果是后者,有没有正确的语法?(至少这是我从中得到的).VC2013对C::()()的通用实现不满意
但它似乎对专业化很满意。例如,使您的实现专门化为
,然后它就会编译。我知道这不符合回答条件,但我希望它能为您的调查添加信息。如果模板按模板更改模板无处不在,这两个示例都适用于clang和g++。但我确实这样做了我不知道如何使用枚举。
$ clang++ -std=c++11 main.cpp && ./a.out
main.cpp:28:23: error: nested name specifier 'A<T>::C<status>::' for declaration does not refer into a class, class template or class template partial specialization
void A<T>::C<status>::operator()() {
~~~~~~~~~~~~~~~~~^
1 error generated.
$ g++-4.8 -std=c++11 -O2 -Wall -pedantic -pthread main.cpp
C: 1
#include <iostream>
#include <string>
#include <sstream>
#include <iostream>
using namespace std;
template<typename T>
struct A {
enum Status {
one = 1
};
template< Status status >
struct C;
};
template<typename T>
template<typename A<T>::Status status >
struct A<T>::C<status> {
/*void operator()() {
cout << "C: " << (int)status << endl;
};*/
void operator()();
};
template<typename T>
template<typename A<T>::Status status >
void A<T>::C<status>::operator()() {
cout << "C: " << status << endl;
}
int main()
{
A<int>::C<A<int>::one> c;
c();
return 0;
}