使用可变参数模板的类成员函数继承

发布于 2025-01-08 06:43:31 字数 1307 浏览 0 评论 0原文

在下面的代码中,Container 的可变参数模板版本似乎没有继承 Container 的单模板版本的 name 函数,g++ 4.5.2 抱怨:

no matching function for call to ”Container<Variable1, Variable2>::name(Variable2)”
candidate is: std::string Container<First_Variable, Rest ...>::name(First_Variable) [with First_Variable = Variable1, Rest = {Variable2}, std::string = std::basic_string<char>]

代码:

#include "iostream"
#include "string"

using namespace std;

struct Variable1 {
    string operator()() {
        return string("var1");
    }
};

struct Variable2 {
    string operator()() {
        return string("var2");
    }
};

template<class... T> class Container;

template<class First_Variable, class... Rest>
class Container<First_Variable, Rest...> : public Container<Rest...> {
public:
    string name(First_Variable variable) {
        return variable();
    }
};

template<class Variable> class Container<Variable> {
public:
    string name(Variable variable) {
        return variable();
    }
};

int main(void) {
    Container<Variable1, Variable2> c;
    cout << "Variables in container: " << c.name(Variable1()) << ", " << c.name(Variable2()) << endl;
    return 0;
}

我做错了什么,或者这是否应该工作?

In the following code it seems that the variadic template version of Container doesn't inherit the name function of the single template version of Container, g++ 4.5.2 complains:

no matching function for call to ”Container<Variable1, Variable2>::name(Variable2)”
candidate is: std::string Container<First_Variable, Rest ...>::name(First_Variable) [with First_Variable = Variable1, Rest = {Variable2}, std::string = std::basic_string<char>]

The code:

#include "iostream"
#include "string"

using namespace std;

struct Variable1 {
    string operator()() {
        return string("var1");
    }
};

struct Variable2 {
    string operator()() {
        return string("var2");
    }
};

template<class... T> class Container;

template<class First_Variable, class... Rest>
class Container<First_Variable, Rest...> : public Container<Rest...> {
public:
    string name(First_Variable variable) {
        return variable();
    }
};

template<class Variable> class Container<Variable> {
public:
    string name(Variable variable) {
        return variable();
    }
};

int main(void) {
    Container<Variable1, Variable2> c;
    cout << "Variables in container: " << c.name(Variable1()) << ", " << c.name(Variable2()) << endl;
    return 0;
}

What am I doing wrong or is this even supposed to work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

等待我真够勒 2025-01-15 06:43:32

name 隐藏了基类的名称。尝试

template<class... T> class Container;

template<class First_Variable, class... Rest>
class Container<First_Variable, Rest...> : public Container<Rest...> {
public:
    using Container<Rest...>::name;

    string name(First_Variable variable) {
        return variable();
    }
};

template<class Variable> class Container<Variable> {
public:
    string name(Variable variable) {
        return variable();
    }
};

如果你是迂腐的,那么你的部分专业化是不正确的。 C++11 规范术语模糊了 形式的两个部分特化。这个问题已经被讨论过,很多人都觉得很惊讶,所以有些编译器没有实现 C++11 的这一部分。

The name are hiding the name of the base class. Try

template<class... T> class Container;

template<class First_Variable, class... Rest>
class Container<First_Variable, Rest...> : public Container<Rest...> {
public:
    using Container<Rest...>::name;

    string name(First_Variable variable) {
        return variable();
    }
};

template<class Variable> class Container<Variable> {
public:
    string name(Variable variable) {
        return variable();
    }
};

If you are pedantic, then your partial specializations are incorrect. The C++11 spec terms ambiguous two partial specializations of the form <FixedParameter, Pack...> and <FixedParameter>. This was discussed and many people find it surprising, so some compilers do not implement that part of C++11.

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