不模板专用化“继承”成员/职能? [英] Doesn't template specialization "inherit" members/functions?

查看:140
本文介绍了不模板专用化“继承”成员/职能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚从C ++开始,我试图创建一个类,扩展basic_string的功能< unsigned char>,让它接受chars作为构造函数的参数,并且他们将通过转换它们并调用basic_string的基础构造函数来处理。



被告知要使用模板专业化,但我一直在努力,因此我已经创建了一个简单的摘要的当前问题:模板专业化如何工作:

  template< typename T> class TestClass {
public:
//示例构造函数
TestClass(const char * name){
std :: cout< 创建类<<名称<< std :: endl;
}
};

//不添加任何功能,只是用作示例
template<> class TestClass< int> {};

正如预期,如果我使用以下:

  TestClass< char> test = TestClass< char>(Test); 

文件编译,输出正确。



但是如果我使用下面的:

  TestClass< int> specializedTest = TestClass< int>(Specialized Test); 

文件未编译,引发以下错误



  test.cpp:在函数'int main()':
test.cpp:50:69:error:no匹配函数调用'TestClass< int> :: TestClass(const char [17])'
test.cpp:50:69:注意:候选人是:
test.cpp:47:注意:testClass< int> :: TestClass()
test.cpp:47:18:note:candidate期望有0个参数,1提供
test.cpp:47:18:note:TestClass< int> :: TestClass(const TestClass< int>&)
test.cpp:47:18:注意:没有从'const char [17]'到'const TestClass< int&

问题1 :专用类不应继承TestClass的成员,功能?因为如果没有,它看起来我只是创建一个类和掩蔽它作为基类。 (在伪代码中)

  class SpecializedTestClass {/ * Add class code here * /}; 
typedef SpecializedTestClass TestClass< int> ;;

如果没有,为什么不呢?对我来说有意义的是,如果你是一个专门的类,所有的类成员/函数应该被保留,除非他们是专门的。



也许我只是混合模板

问题2 :这是我的问题的最佳方法吗? (结合basic_string< unsigned char>和basic_string< char>的功能,使数据存储为unsigned char,但可以使用typecast从char中赋值。)

解决方案

否。当你(明确地或部分地)专门化一个类时,你的专业化是一个全新的模板。它与现有模板共享的唯一的是它的名称。



至于为什么,我们只能猜测。两个明显的原因是,它是相当困难的(大多数编译器没有正确地实施当前的部分专业化规范多年后,标准写),并且它可能是开放的问题(最多)是否是可取的。 / p>

至于这是最好的方法:我怀疑它,但我没有更好的建议。获取 string / basic_string 可以很好地处理 unsigned char 一直是一个问题,因为它是发明的,我还没有看到一种方法来处理它,但产生特别好的结果。


I'm just starting with C++, and I'm trying to create a class that extends the functionalities of basic_string < unsigned char > to let it accept chars as arguments for the constructors too, and they'd be handled by casting them and calling the base constructors for basic_string.

I've been told to use template specialization for that, but I've been struggling with it, so I've created a simple extract of the current problem: how template specialization works:

template<typename T> class TestClass{
  public:
    // Sample constructor
    TestClass(const char* name){
      std::cout << "Created class " << name << std::endl;
    }
};

// Doesn't add any functionalities, just used as an example
template<> class TestClass<int>{};

As expected, if I use the following:

  TestClass<char> test = TestClass<char>("Test");

The file compiles, and the output is correct.

But if I use the following:

  TestClass<int> specializedTest = TestClass<int>("Specialized Test");

The file doesn't compile, throwing the following error:

test.cpp: In function ‘int main()’:
test.cpp:50:69: error: no matching function for call to ‘TestClass<int>::TestClass(const char [17])’
test.cpp:50:69: note: candidates are:
test.cpp:47:18: note: TestClass<int>::TestClass()
test.cpp:47:18: note:   candidate expects 0 arguments, 1 provided
test.cpp:47:18: note: TestClass<int>::TestClass(const TestClass<int>&)
test.cpp:47:18: note:   no known conversion for argument 1 from ‘const char [17]’ to ‘const TestClass<int>&’

Question 1: Shouldn't the specialized class inherit the TestClass's members and functions? Because if it doesn't, it looks to me that I'm just creating a class and "masking" it as the base class. i.e. (in pseudocode)

class SpecializedTestClass{/* Add class code here */};
typedef SpecializedTestClass TestClass<int>;

If not, why not? It makes sense for me that, if you're specializing a class, all classes members/functions should be kept unless they are specialized.

Maybe I'm just mixing template with class inheritance.

Question 2: Is this the best approach to my problem? (Combine the functionalities of basic_string< unsigned char > and basic_string< char > so that the data is stored as unsigned char, but can be assigned from char using typecasts.)

解决方案

No. When you (explicitly or partially) specialize a class, your specialization is an entirely new template. The only thing it shares with the existing template is its name.

As to why, we can only guess. Two obvious reasons would be that it's fairly difficult (to the point that most compilers didn't implement the current partial specialization spec correctly for years after the standard was written) and that it's probably open to question (at best) whether it's desirable.

As far as this being the best approach: I doubt it, but I don't have a lot better suggestion to give either. Getting string/basic_string to deal nicely with unsigned char has been a problem almost since it was invented, and I haven't seen a way to deal with it yet that produces particularly good results.

这篇关于不模板专用化“继承”成员/职能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆