Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.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++_Templates_Overloading_C++14_Template Specialization - Fatal编程技术网

C++ 区分用户和非用户类型&;模板专门化

C++ 区分用户和非用户类型&;模板专门化,c++,templates,overloading,c++14,template-specialization,C++,Templates,Overloading,C++14,Template Specialization,我需要以某种方式重载用户和非用户类型的类模板。问题源于template类从template参数继承的点: #include <iostream> #include<string> #include<map> #include<type_traits> #include<string> using namespace std; class Broken_imput{};//exception class class No_such_cl

我需要以某种方式重载用户和非用户类型的类模板。问题源于template类从template参数继承的点:

#include <iostream>
#include<string>
#include<map>
#include<type_traits>
#include<string>
using namespace std;

class Broken_imput{};//exception class
class No_such_class{};

class Io_obj; // For MAP to know about this type

//MAP: KEY - String, VALUE - function pointer
map<string,Io_obj*(*)(istream&)> func_storage;

bool read_string(istream& ss,string& str)
{
    if(ss>>str)return true; // read string from the given source
    return false;
}
//------------------------------------------------
//------------------------------------------------
////CLASS THAT SETS THE INTERFACE///////////////
class Io_obj
{
public:

    virtual ~Io_obj(){}
    virtual void print_data()= 0;
};

//-------------------------------------------------
//FUNCTION THAT READS OBJECTS LOOKS FOR THE APPROPRIATE  METHOD IN MAP AND CALLS
// IT IF METHOD EXIST. METHOD READS OBJECT OF THE TYPE IT KNOWS FROM GIVEN STREAM
Io_obj* read_object(istream& stream)
{
    string str;
    if(!read_string(stream,str)) throw Broken_imput{};
    if(auto FN=func_storage[str])return FN(stream);
    throw No_such_class{};

}
////////////////SOME CLASSES//////////////////////////////////

struct Shape
{
    string data;
};

struct Circle:public Shape{};

template < class T,class = enable_if_t<is_class<U>::value,U >>
class Io:public T,public Io_obj
{
public:
    Io(istream& stream){ stream>>T::data;}
    void print_data() override
    {
        cout<<T::data<<endl;
    }

    //read value
   static  Io_obj* read_value(istream& stream) { return new Io{stream};} //deligate to constructor

};
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
类中断_输入{}//例外类
类{}类{};
类别Io_obj;//让MAP了解此类型
//映射:键-字符串,值-函数指针
地图功能存储;
bool read_字符串(istream&ss、string&str)
{
if(ss>>str)返回true;//从给定源读取字符串
返回false;
}
//------------------------------------------------
//------------------------------------------------
////类来设置接口///////////////
Io_obj类
{
公众:
虚拟~Io_obj(){}
虚空打印_数据()=0;
};
//-------------------------------------------------
//读取对象的函数在映射中查找适当的方法并调用
//如果方法存在的话,它就不存在了。方法从给定流中读取它知道的类型的对象
Io_对象*读取对象(istream和stream)
{
字符串str;
如果(!read_string(stream,str))抛出断开的_输入{};
if(auto FN=func_storage[str])返回FN(stream);
不要抛出这样的类{};
}
////////////////一些班级//////////////////////////////////
结构形状
{
字符串数据;
};
结构圆:公共形状{};
模板
Io类:公共T、公共Io_obj
{
公众:
Io(istream&stream){stream>>T::data;}
void print_data()覆盖
{

不能更仔细地查看标准标题中的设施
。例如
std::is_class::value
is
true
如果
X
是非联合类类型,则
false
否则。这些设施可用于支持您所寻求的专业化。最简单的方法是

template<class T, bool = std::is_class<T>::value>
class Io:public T,public Io_obj
{ /* ... */ };


template<class T>
class Io<T, false> : public Io_obj
{ /* ... */ };

谢谢!这就是我想要的:)你能解释一下如何使用enable_if解决这个问题吗?(很快,只是为了满足我的好奇心:)
template<class T, class = void>
class Io:public T,public Io_obj
{ /* ... */ };


template<class T>
class Io<T, std::enable_if_t<!std::is_class<T>::value>> : public Io_obj
{ /* ... */ };