Cleanest way for conditional code instantiation in C++ template

后端 未结 6 1557
深忆病人
深忆病人 2021-02-18 19:11

I\'m trying to get the following C++ code running:

#include 

template class ConditionalData {
};

template 

        
6条回答
  •  臣服心动
    2021-02-18 19:27

    you can define setData for both branches, empty one for false condition:

    template class ConditionalData {
    };
    
    template  class ConditionalData {
        void setData(T _data) {}
    };
    
    template  class ConditionalData {
    private:
        T data;
    public:
        void setData(T _data) { data = _data; }
    };
    
    template class A {
    public:
        A() {
            ConditionalData data;
            data.setData(3);
        }
    };
    

提交回复
热议问题