c模板中的多个类型名参数? (可变参数模板)

新手上路,请多包涵

如何在 C++ 模板中有多个类型名参数?

 #ifndef _CALL_TEMP_H
#define _CALL_TEMP_H

#include <string>
#include <iostream>

template <typename Sig>
class Foo;

template <typename A, typename B>
class Foo
{
    public:
        void output() {
            std::cout << a_ << b_ << std::endl;
        }
        A a_;
        B b_;
};

template <typename A, typename B, typename C>
class Foo
{
    public:
        void output() {
            std::cout << a_ << b_ << c_ << std::endl;
        }
        A a_;
        B b_;
        C c_;
};

#endif

用法:

 int main()
{
    Foo<int ,int> doubleint;
    doubleint.a_ = 1;
    doubleint.b_ = 2;
    doubleint.output();
//  Foo<int , int , std::string> comp;
//  comp.a_ = 1;
//  comp.b_ = 2;
//  comp.c_ = "haha";
//  comp.output();
    return 0;
}

但它不会编译。我怎样才能让它编译?

原文由 Jichao 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 641
2 个回答

只需使用可变参数模板声明一个主模板,然后专门针对每个支持的模板参数数量。例如:

 #ifndef CALL_TEMP_H
#define CALL_TEMP_H

#include <iostream>

template <typename...> class Foo;

template <typename A, typename B>
class Foo<A, B>
{
public:
    void output() {
        std::cout << a_ << b_ << '\n';
    }
    A a_;
    B b_;
};

template <typename A, typename B, typename C>
class Foo<A, B, C>
{
public:
    void output() {
        std::cout << a_ << b_ << c_ << '\n';
    }
    A a_;
    B b_;
    C c_;
};

#endif

我不能使用 C++11,并且想要保留类似的符号,您需要使用模板默认参数来模拟可变参数列表。这将隐式限制模板参数的数量,但由于无论如何您都在专门化模板,所以这个限制并不重要。

如果可以接受使用不同的符号,您还可以使用看起来像函数声明的东西来实例化和专门化您的模板:

 template <typename> class Foo;

template <typename A, typename B>
class Foo<void(A, B)> {
    ...
};
template <typename A, typename B, typename C>
class Foo<void(A, B, C)> {
    ...
};
...
Foo<void(int, int)>                   f2;
Foo<void(int, int, std::string)> f3;

符号的更改是否可以接受取决于您对类模板的使用。但是,如果没有 C++11,您将无法获得理想的解决方案。

顺便说一句, 不要过度使用 std::endl :使用 '\n' 表示行尾。如果您真的要刷新流,请使用 std::flush 。此外, _CALL_TEMP_H 是标准 C++ 库保留的名称,所有以下划线后跟大写字符的名称也是如此:除非有使用它们的明确许可,否则 不要 在您自己的代码中使用这些名称(例如 __FILE____LINE__ 被保留,但已授予使用它们的明确许可)。

原文由 Dietmar Kühl 发布,翻译遵循 CC BY-SA 3.0 许可协议

如果您有多个版本的模板,则必须专门化一个版本。如果您想要不同数量的参数,那么诀窍是使用标记类说“此参数没有参数”,并将其作为默认参数。

在您的情况下,类似以下工作(编译和测试):

 #include <iostream>

// tag class indicating "no member in this place"
struct nothing {};

template <typename A, typename B, typename C = nothing> // <- note default arg.
class Foo;

template <typename A, typename B>
class Foo<A, B, nothing> // <- note specialization
{
    public :
        void output() {
            std::cout << a_ << b_ << std::endl;
        }

        A a_;
        B b_;
};

template <typename A, typename B, typename C>
class Foo
{
    public :
        void output() {
            std::cout << a_ << b_ << c_ << std::endl;
        }

        A a_;
        B b_;
        C c_;
};

int main()
{
    Foo<int, int> doubleint;
    doubleint.a_ = 1;
    doubleint.b_ = 2;
    doubleint.output();

    Foo<int, int, int> tripleint;
    tripleint.a_ = 1;
    tripleint.b_ = 2;
    tripleint.c_ = 3;
    tripleint.output();
}

请注意,这本质上是对 boost::tuple<>/std::tuple<> 的重新发明,您绝对应该阅读它。

原文由 Kaz Dragon 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
logo
Stack Overflow 翻译
子站问答
访问
宣传栏