简体   繁体   English

模板特化基类函数模板缺失

[英]Template specialization base class function template missing

Consider the following:考虑以下:

#include <iostream>
#include <string>

/**
 * Provides base functionality for any property.
 */
struct property_base
{
    virtual std::string to_string() const = 0;
    
protected:
    void notify() { std::cout << "notifying!" << std::endl; }    
};

/**
 * Generic property implementation template.
 */
template<typename T>
struct property_impl :
    property_base
{
    T data;
    
    property_impl<T>& operator=(const T& t)
    {
        this->data = t;
        this->notify();
        return *this;
    }
};

/**
 * Generic property template.
 */
template<typename T>
struct property :
    property_impl<T>
{
};

/**
 * 'int' property specialization
 */
template<>
struct property<int> :
    property_impl<int>
{
    std::string to_string() const { return std::to_string(data); }
};

/**
 * `std::string` property specialization
 */
template<>
struct property<std::string> :
    property_impl<std::string>
{
    std::string to_string() const { return data; }  
};

int main()
{
    property<int> x;
    property<std::string> str;
    
    x = 42;
    str = "Hello World!";
    
    return 0;
}

When compiling this, the compiler complains about not finding a match for operator= with operand types property<int> and int .编译时,编译器抱怨找不到操作数类型为property<int>int operator=的匹配项。 As I understand the problem is that I'm calling property<int>::operator=(int) which does not exist.据我了解,问题是我正在调用不存在的property<int>::operator=(int) Instead, I only have property_impl<int>::operator(int) defined.相反,我只定义了property_impl<int>::operator(int)

Is there a way of making this work without requiring each property<T> template specialization to explicitly implement the operator=() ?有没有办法在不需要每个property<T>模板特化来显式实现operator=()的情况下完成这项工作? The implementation of operator= will be the same for all specialization so I'm looking for a way of not requiring an explicitly implemented operator= for all future property<T> specializations. operator=的实现对于所有专业化都是相同的,所以我正在寻找一种不需要为所有未来的property<T>专业化明确实现的operator=的方法。

Coliru link to thinker: http://coliru.stacked-crooked.com/a/1db9165e4f78ffa4 Coliru 链接到思想家: http ://coliru.stacked-crooked.com/a/1db9165e4f78ffa4

Very few things in C++ happen automatically. C++ 中很少有事情会自动发生。 Fortunately, in this case you don't have to write a lot of additional code, only add a using declaration to each subclass:幸运的是,在这种情况下,您不必编写很多额外的代码,只需为每个子类添加一个using声明:

/**
 * 'int' property specialization
 */
template<>
struct property<int> :
    property_impl<int>
{
    using property_impl<int>::operator=;
    std::string to_string() const { return std::to_string(data); }
};

/**
 * `std::string` property specialization
 */
template<>
struct property<std::string> :
    property_impl<std::string>
{
    using property_impl<std::string>::operator=;
    std::string to_string() const { return data; }
};

You will have to explicitly add the using declaration to this subclass, but that's still better than having to copy/paste the same operator= in each one of them.您必须显式地将using声明添加到这个子类,但这仍然比在每个子类中复制/粘贴相同的operator=更好。

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

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