使用 C++03 模拟 Variadic 模板时,我们可以在类中使用不同的代码吗?

发布于 2024-12-20 10:13:40 字数 1883 浏览 6 评论 0原文

我试图根据模板参数用不同的代码填充我的类,但出现编译错误。我的代码是这样的:

#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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只是在用心讲痛 2024-12-27 10:13:40

您需要先将类的构造函数设为public,然后才能实例化它们。

You need to make the constructors of your class public before they can be instantiated.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文