Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/125.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;-恢复模板或强制转换为模板_C++_Templates_C++11 - Fatal编程技术网

C++ C++;-恢复模板或强制转换为模板

C++ C++;-恢复模板或强制转换为模板,c++,templates,c++11,C++,Templates,C++11,考虑以下代码清单: #include <iostream> #include <typeinfo> class Interface { }; template<typename T> class Class : public Interface { }; template<typename T> Interface *producer() { std::cout << "produced " << typeid

考虑以下代码清单:

#include <iostream>
#include <typeinfo>

class Interface { };

template<typename T>
class Class : public Interface { };

template<typename T>
Interface *producer() {
    std::cout << "produced " << typeid(T).name();
    return new Class<T>();
}

template<typename T>
void consumer(Class<T> *class_value) {
    std::cout << "consumed " << typeid(T).name();
}

void mediator(Interface *value) {
    /* The magic happens here */
    consumer(class_value);
}

int main() {
    Interface *value = producer<int>();
    mediator(value);
}
#包括
#包括
类接口{};
模板
类:公共接口{};
模板
接口*producer(){

std::cout您可以使用
dynamic\u cast

consumer(dynamic_cast<producer<int>&>(value));
consumer(动态_cast(值));

如果您稍微更改一下设计,可以让派生类完成以下工作:

class Interface
{
    virtual void consume() = 0;
    virtual ~Interface() {}
};

void mediator(Interface * value)
{
    value->consume();
}

template <typename T>
void consumer(Class<T> * class_value)
{
    std::cout << "consumed " << typeid(T).name();
}

template <typename T> class Class : public Interface
{
    virtual void consume() override
    { 
        consumer<Class>(this);
    }
    // ...
};
类接口
{
虚空消耗()=0;
虚拟~Interface(){}
};
无效中介(接口*值)
{
值->消费();
}
模板
无效消费者(类别*类别值)
{

std::cout直接答案

您发布的代码似乎是为了非法使用cast而编写的,添加合适的cast似乎是直接的答案

void mediator(Interface *value) {
    /* The magic happens here */
    Class<int> *class_value(static_cast<Class<int> *>(value));
    consumer(class_value);
}
双重分派

按照Kerrek SB的建议更改设计,可以提供更干净、更可扩展的解决方案

void mediator(Interface *value) {
    /* The magic happens here */

    {
        Class<int> *class_value(dynamic_cast<Class<int> *>(value));
        if(class_value) {
            consumer(class_value);
            return;
        }
    }

    {
        Class<float> *class_value(dynamic_cast<Class<float> *>(value));
        if(class_value) {
            consumer(class_value);
            return;
        }
    }
}