Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/141.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_Inheritance - Fatal编程技术网

C++ 具有模板成员的模板类

C++ 具有模板成员的模板类,c++,templates,inheritance,C++,Templates,Inheritance,我的班级结构如下 class Base { protected: template <typename Type> Type convert(); // no implementation public: template <typename Type> operator Type() { Type t(convert<Type>()); // log conversion ret

我的班级结构如下

class Base {
  protected:
    template <typename Type>
    Type convert(); // no implementation

  public:
    template <typename Type>
    operator Type() { 
      Type t(convert<Type>());
      // log conversion
      return t;
    }
  }
};

class D1: public Base
{
   protected:
     template <type Type>
     Type convert() {
       Type t;
       // Set t
       return t;
     }
 };

 template <typename Type>
 class D2: public Base
 {
    public:
      D2(Type v): value(v) {}

    protected:
      template <typename Type2>
      Type2 convert() {
          return value;
      }

    private:
      Type value;

 }
但很明显

我也试过了

template <>
template <typename Type>
Type D2<Type>::convert<Type>() { ... }
模板
样板
类型D2::convert(){…}
抱怨“Type D2::convert()const”的原型与类“D2”中的任何原型都不匹配”

我无法在转换时删除模板,因为D1需要它

有没有办法让它发挥作用

在高水平上,我需要能够做到以下几点:

Base *b = new D1(/*stuff*/);
int i = *b; // calls D1::convert<int>                  (a)
b = new D1(/*different stuff*/);
std::string s = *b; // calls D1::convert<std::string>  (b)
b = new D2<int>(/* other stuff */);
int j = *b; // calls D2<int>::convert<int>             (c)
b = new D2<std::string>(/*still other args*/);
s = *b; // calls D2<std::string>::convert<std::string> (d)

// This should fail as a compile time or link time error
b = new D2<int>(/*... */);
std::string s_fail = *b; // convertable to int, not string
   // tries to call D2<int>::convert<std::string>, which is either 
   // not implemented or has an implementation that fails to compile, 
   // probably by casting an int to string             (e)
Base*b=新的D1(/*stuff*/);
int i=*b;//调用D1::convert(a)
b=新D1(/*不同材料*/);
std::string s=*b;//调用D1::convert(b)
b=新D2(/*其他材料*/);
int j=*b;//调用D2::convert(c)
b=新的D2(/*还有其他参数*/);
s=*b;//调用D2::convert(d)
//这将作为编译时或链接时错误而失败
b=新的D2(/*…*/);
std::string s_fail=*b;//可转换为int,而不是string
//尝试调用D2::convert,它是
//未实现或具有无法编译的实现,
//可能通过将int转换为字符串(e)
期望的行为是:

(a) 调用D1中返回int的方法
(b) 调用D1中返回字符串的方法
(c) 调用D2中返回int的方法
(d) 调用D2中返回字符串的方法
(e) 尽早失败,最好是在编译时或链接时

这不是C++中的可能吗?我很乐意重写所有的东西,除了BASIC的公共接口,D1不是模板化的,D2是模板化的。


编辑:澄清我想要的行为

因此,
D1
可以转换为
int
string
,但是
D2
,从
D1
派生,应该只能转换为
int
,这违反了LSP,并且因为您使用了指向基类的指针(
b
)这只能在运行时检测到。
std::string s=*b
在编译时不能同时出现错误和成功,这取决于
b
的动态类@dyp D2是从Base派生的,而不是D1。@TroyDaniels啊,好的。问题仍然是:您只能通过指向
b的指针调用
b
中声明的函数
@Jarod42如果我有D2::convert和D2::convert的专门化,但没有混合案例,那么当我尝试链接
std::string s=*b
b
时会发生什么情况,而
b
是由
b=new D2
创建的?它似乎应该查找
Base::convert
D2::convert
,并且两者都找不到,所以链接会失败困扰
Base *b = new D1(/*stuff*/);
int i = *b; // calls D1::convert<int>                  (a)
b = new D1(/*different stuff*/);
std::string s = *b; // calls D1::convert<std::string>  (b)
b = new D2<int>(/* other stuff */);
int j = *b; // calls D2<int>::convert<int>             (c)
b = new D2<std::string>(/*still other args*/);
s = *b; // calls D2<std::string>::convert<std::string> (d)

// This should fail as a compile time or link time error
b = new D2<int>(/*... */);
std::string s_fail = *b; // convertable to int, not string
   // tries to call D2<int>::convert<std::string>, which is either 
   // not implemented or has an implementation that fails to compile, 
   // probably by casting an int to string             (e)