简体   繁体   English

如何获取模板 class 的值?

[英]How to get the value of a template class?

I'm trying to get the value of a template class. To get the value of a class, I can easily do like:我正在尝试获取模板 class 的值。要获取 class 的值,我可以轻松地执行以下操作:

int get_value()
{
    return *this;
}

But I want to create a class, and extend it, and don't make get_value() in all classes again.但我想创建一个 class,并扩展它,并且不要在所有类中再次创建get_value() So, I did that:所以,我这样做了:

template<typename T>
class _extend : public T
{
    public:
        auto _get_value()
        {
            return *this;
        }
};
template<typename T>
class extend : public _extend<T>
{
    public:
        T get_value()
        {
            auto value = this->_get_value(); /* It was `_get_value()`, changed to `this->_get_value()` due to the comments */
            T output = value;
            return output;
        }
};

But it's not working: output is nothing.但它不起作用:output 什么都不是。

Edit编辑

Sample program:示例程序:

#include <iostream>

namespace kc
{
    template<typename T>
    class _extend : public T
    {
        public:
            auto _get_value()
            {
                return *this;
            }
    };
    template<typename T>
    class extend : public _extend<T>
    {
        public:
            T get_value()
            {
                auto value = this->_get_value();
                T output = value;
                return output;
            }
    };
    class A : public std::string, public extend<std::string>
    {
        public:
            using std::string::string;
    };
}

int main()
{
    kc::A a("a");
    std::cout << a.get_value() << std::endl;
}

The problem with the code in the question is the multiple inheritance.问题中代码的问题是多个inheritance。 The *this that _extend<string>::get_value() will be called with is not the string that has a value. _extend<string>::get_value()将被调用的*this不是具有值的字符串。

Although I agree with @NicolBolas in comments in that I don't understand why you want this, but you can make a class template that will return the value of what it derives from with only one level of inheritance.尽管我同意@NicolBolas 在评论中的观点,因为我不明白您为什么要这样做,但是您可以制作一个 class 模板,该模板将仅返回一层 inheritance 的值。 You just need to give it a perfect forwarding constructor and have get_value() cast to the base type, ie您只需要给它一个完美的转发构造函数并将get_value()转换为基本类型,即

#include <iostream>
#include <string>

namespace kc
{
    template<typename T>
    class extend : public T
    {
    public:

        template<typename... Args>
        extend(Args&&... args) : T(std::forward<Args>(args)...) 
        {}

        T get_value()
        {
            return *static_cast<T*>(this);
        }
    };

}

int main()
{
    kc::extend<std::string> a("foo");
    std::cout << a.get_value() << std::endl;
}

Basically:基本上:

template<class C>
class extend : public C
{
    public:
        using C::C;
        auto get()
        {
            return *this;
        }
};

And a full example:一个完整的例子:

#include <iostream>

template<class C>
class extend : public C
{
    public:
        using C::C;
        auto get()
        {
            return *this;
        }
};

class e_string : public extend<std::string>
{
    public:
        using extend<std::string>::extend;
};

int main()
{
    e_string s = "Test";
    std::cout << s.get() << std::endl;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM