使用 C++03 模拟 Variadic 模板时,我们可以在类中使用不同的代码吗?
我试图根据模板参数用不同的代码填充我的类,但出现编译错误。我的代码是这样的:
#include <iostream>
#include <string>
struct EmptyType { };
template<class arg1=EmptyType, class arg2=EmptyType, class arg3=EmptyType>
class my_class
{
my_class(){
std::cout << 3 << std::endl;
}
// FILL_MY_CLASS_DEFINE(3)
};
template<class arg1, class arg2>
class my_class<arg1,arg2,EmptyType>
{
my_class(){
std::cout << 2 << std::endl;
}
// FILL_MY_CLASS_DEFINE(2)
};
template<class arg1>
class my_class<arg1,EmptyType,EmptyType>
{
my_class(){
std::cout << 1 << std::endl;
}
};
template<>
class my_class<EmptyType,EmptyType,EmptyType>
{
// FILL_MY_CLASS_DEFINE(0)
};
int main(int argc, const char *argv[])
{
my_class<std::string, double, int> a;
my_class<std::string, int> b;
my_class<void> c;
//my_class d;
return 0;
}
我收到很多错误:
prog.cpp: In function ‘int main(int, const char**)’:
prog.cpp:9: error: ‘my_class<arg1, arg2, arg3>::my_class() [with arg1 = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, arg2 = double, arg3 = int]’ is private
prog.cpp:38: error: within this context
prog.cpp:17: error: ‘my_class<arg1, arg2, EmptyType>::my_class() [with arg1 = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, arg2 = int]’ is private
prog.cpp:39: error: within this context
prog.cpp:25: error: ‘my_class<arg1, EmptyType, EmptyType>::my_class() [with arg1 = void]’ is private
prog.cpp:40: error: within this context
实时代码此处。所以我想知道:当使用 C++03 模拟 Variadic 模板时,类中是否可以有不同的代码?
I am trying to fill my classes with different code depending on template arguments but get a compilation error. My code is like this:
#include <iostream>
#include <string>
struct EmptyType { };
template<class arg1=EmptyType, class arg2=EmptyType, class arg3=EmptyType>
class my_class
{
my_class(){
std::cout << 3 << std::endl;
}
// FILL_MY_CLASS_DEFINE(3)
};
template<class arg1, class arg2>
class my_class<arg1,arg2,EmptyType>
{
my_class(){
std::cout << 2 << std::endl;
}
// FILL_MY_CLASS_DEFINE(2)
};
template<class arg1>
class my_class<arg1,EmptyType,EmptyType>
{
my_class(){
std::cout << 1 << std::endl;
}
};
template<>
class my_class<EmptyType,EmptyType,EmptyType>
{
// FILL_MY_CLASS_DEFINE(0)
};
int main(int argc, const char *argv[])
{
my_class<std::string, double, int> a;
my_class<std::string, int> b;
my_class<void> c;
//my_class d;
return 0;
}
I get lots of errors:
prog.cpp: In function ‘int main(int, const char**)’:
prog.cpp:9: error: ‘my_class<arg1, arg2, arg3>::my_class() [with arg1 = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, arg2 = double, arg3 = int]’ is private
prog.cpp:38: error: within this context
prog.cpp:17: error: ‘my_class<arg1, arg2, EmptyType>::my_class() [with arg1 = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, arg2 = int]’ is private
prog.cpp:39: error: within this context
prog.cpp:25: error: ‘my_class<arg1, EmptyType, EmptyType>::my_class() [with arg1 = void]’ is private
prog.cpp:40: error: within this context
Live code here. So I wonder: is it possible to have different code inside a class when emulating Variadic templates with C++03?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要先将类的构造函数设为
public
,然后才能实例化它们。You need to make the constructors of your class
public
before they can be instantiated.