使用C++模板切换算法

我有一个具有pointQuery函数的C++类。目前,当我必须在线性搜索和二进制搜索之间切换时,我会注释代码的另一部分。我看到了this的帖子,并试图重构我的代码。我的代码是这样的:

namespace searchStrategy{
    struct linearSearch{};    
    struct binarySearch{};
}

template<typename T, typename LookupStrategy> class leaf:public rtNode<T>{
    public:T points[maxCapLeaf][d]; // search to be done on this array
    // some other class members, most of which used in the search

    template<typename searchStrategy::linearSearch> bool pointQuery(const T* f) const{
        // some code
    }

    template<typename searchStrategy::binarySearch> bool pointQuery(const T* f) const{
        // some code
    }

};

类的对象被创建为:

leaf<T, LookupStrategy>* temp = new leaf<T, LookupStrategy>(some_input_params);

其中LookupStrategysearchStrategy::binarySearchsearchStrategy::linearSearch

当我编译它时,我得到了以下错误:

/file_address/template.cpp:164:39: error: non-type template parameters of class type only available with ‘-std=c++2a’ or ‘-std=gnu++2a’
  164 |     template<typename searchStrategy::linearSearch> bool pointQuery(const T* f) const{
      |                                       ^~~~~~~~~~~~
/file_address/template.cpp:200:39: error: non-type template parameters of class type only available with ‘-std=c++2a’ or ‘-std=gnu++2a’
  200 |     template<typename searchStrategy::binarySearch> bool pointQuery(const T* f) const{
      |                                       ^~~~~~~~~~~~
/file_address/template.cpp:200:58: error: ‘template<class T, class LookupStrategy> template<<typeprefixerror><anonymous> > bool leaf<T, LookupStrategy>::pointQuery(const T*) const’ cannot be overloaded with ‘template<class T, class LookupStrategy> template<<typeprefixerror><anonymous> > bool leaf<T, LookupStrategy>::pointQuery(const T*) const’
  200 |     template<typename searchStrategy::binarySearch> bool pointQuery(const T* f) const{
      |                                                          ^~~~~~~~~~
/file_address/template.cpp:164:58: note: previous declaration ‘template<class T, class LookupStrategy> template<<typeprefixerror><anonymous> > bool leaf<T, LookupStrategy>::pointQuery(const T*) const’
  164 |     template<typename searchStrategy::linearSearch> bool pointQuery(const T* f) const{
      |

有人能解释一下我做错了什么吗?我该如何解决这个问题?(请注意,函数需要保留在类本身中,我不能将其转移到名称空间,因为在搜索函数中使用了许多类成员)谢谢……

转载请注明出处:http://www.haohaidanqing.com/article/20230526/1660498.html