简体   繁体   English

非模板类中的强制模板方法

[英]Force template method in non-template class

I try to achieve the following behavior/syntax/usage of this class: 我尝试实现此类的以下行为/语法/用法:

Data1 dataType1;
Data2 dataType2;

int intType;
float floatType;

dataType1.method( intType );
dataType1.method( floatType );

dataType2.method( intType );
dataType2.method( floatType );

My approach would be this: 我的方法是这样的:

struct CDataBase
{
    template< typename T > virtual void method( T type ) = 0;
};

struct CData1 : CDataBase
{
    template< typename T > void method( T type ) {}
};

struct CData2 : CDataBase
{
    template< typename T > void method( T type ) {}
};

However virtual template methods aren't possible. 但是虚拟模板方法是不可能的。 Also there is no need for an actual base class, However I have to ensure that some classes got a (template) 'method()' implemented. 同样也不需要实际的基类,但是我必须确保某些类实现了(模板)“ method()”。

How do I force a non-templated class/struct to override a template method? 如何强制非模板化的类/结构重写模板方法?


EDIT: This is my actual layout: 编辑:这是我的实际布局:

struct Data0
{
    int someVar;

    template< class T >
    void decode( T& type )
    {
        type.set( someVar );
    }
};

EDIT: in the current version of C++ (11) the behavoir I try to achieve isn't possible. 编辑:在当前版本的C ++(11)中,我尝试实现的行为是不可能的。 In addition to that, I should really recode this part to avoid this problem. 除此之外,我真的应该重新编码这部分以避免此问题。 However I accept the only answer given, thanks for you affort. 但是,我接受给出的唯一答案,谢谢您的努力。

The basic idea to check for specific functions implemented of a given template parameter type, is to try instantiate function pointers of these. 检查给定模板参数类型实现的特定功能的基本思想是尝试实例化这些功能的指针。 The compiler will complain, if the function pointer initializations cannot be resolved. 如果无法解析函数指针的初始化,编译器将抱怨。

Here's some sample code to illustrate the principle: 这是一些示例代码来说明原理:

template<typename T>
void check_has_foo_function() {
    void (T::*check)(int, double) = &T::foo;
    (void)check;
}

struct A {
    void foo(int, double) {};
};

struct B {
    void bar(int, double) {};
};

template<typename CheckedClass>
struct Client {
    void doSomething() {
        check_has_foo_function<CheckedClass>();
        CheckedClass x;
        x.foo(5,3.1415);
    }
};

int main() {

    Client<A> clientA;
    clientA.doSomething();

    // Uncomment the following lines to see the compilation fails
    // Client<B> clientB;
    // clientB.doSomething();
    return 0;
}

Note the call to the check_has_foo_function<CheckedClass>(); 请注意对check_has_foo_function<CheckedClass>();的调用check_has_foo_function<CheckedClass>(); function will be completely optimized out, and doesn't have any impact on runtime performance. 函数将被完全优化,并且对运行时性能没有任何影响。

Based upon this, further abstractions could be provided (eg to generate checks using preprocessor macros). 基于此,可以提供其他抽象(例如,使用预处理器宏生成检查)。 I have published a little experimental framework on GitHub that uses these techniques. 我在GitHub上发布了一个使用这些技术的实验框架

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

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