编译时检查模板类型C ++

时间:2014-02-27 23:46:12

标签: c++ templates std

我正在尝试检查模板类型并适当调用一个函数。但是,这似乎不起作用。我尝试使用is_same,C++ compile-time type checkingcompile-time function for checking type equality和boost :: is_same。一切都给了我同样的错误。以下是示例代码。

#include <iostream>
#include <type_traits>
using namespace std;

class Numeric
{
public :
    bool isNumeric()
    {
    return true;
    }
};
class String
{

};

template <class T>
class Myclass
{
    private:
    T temp;
    public:
    void checkNumeric()
    {
        if(std::is_same<T,Numeric>::value)
        {
            cout << "is numeric = " << temp.isNumeric();
        }
        else
        {
            cout << "is numeric = false" << endl;
        }
    }

};

int main()
{
    Myclass<Numeric> a;
    a.checkNumeric();
    Myclass<String> b;
    b.checkNumeric();
}

在编译上面的代码时,我收到以下错误。

make all 
Building file: ../src/TestCPP.cpp
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/TestCPP.d" -MT"src/TestCPP.d" -o "src/TestCPP.o" "../src/TestCPP.cpp"
../src/TestCPP.cpp:36:36: error: no member named 'isNumeric' in 'String'
                    cout << "is numeric = " << temp.isNumeric();
                                               ~~~~ ^
../src/TestCPP.cpp:51:4: note: in instantiation of member function               'Myclass<String>::checkNumeric' requested here
    b.checkNumeric();
      ^
1 error generated.
make: *** [src/TestCPP.o] Error 1

在这种情况下,我既没有String也没有Numeric类。它来自第三方图书馆。我只实现了MyClass,它将被打包为另一个库。我希望使用MyClass的应用程序将传递给属于第三方类的String或Numeric。 MyClass是一个专门的矩阵操作,Dense / Sparse矩阵是来自第三方库的类数字和字符串类。我想检查使用我的库和第三方库的应用程序是否根据属于第三方库的类类型调用MyClass。

请让我知道如何解决这个问题。

5 个答案:

答案 0 :(得分:9)

不需要做任何花哨的事;这可以使用普通的模板专业化来处理。

template <class T>
class Myclass
{
    private:
    T temp;

    public:
    // called for general T
    void checkNumeric() {
        cout << "is numeric = false" << endl;
    }
};

// specialized version for T = Numeric
template<> void Myclass<Numeric>::checkNumeric() {
    cout << "is numeric = " << temp.isNumeric() << endl;
}

答案 1 :(得分:4)

您需要在编译期间选择启用/禁用功能,而不是在运行时选择。我建议做类似的事情(code on ideone.com):

#include <iostream>
#include <type_traits>

class Numeric {
 public:
  bool isNumeric() {
   return true;
  }
};

class String {
};

template<class T>
class Myclass {
 private:
  T temp;
 public:
  template<typename U = T, typename std::enable_if<std::is_same<U, Numeric>::value, std::size_t>::type = 0>
  void checkNumeric() {
   std::cout << "is numeric = " << temp.isNumeric() << std::endl;
  }

  template<typename U = T, typename std::enable_if<!std::is_same<U, Numeric>::value, std::size_t>::type = 0>
  void checkNumeric() {
   std::cout << "is numeric = false" << std::endl;
  }
};

int main() {
 Myclass<Numeric> a;
 a.checkNumeric();
 Myclass<String> b;
 b.checkNumeric();
}

节目输出:

is numeric = 1
is numeric = false

希望这有帮助。

答案 2 :(得分:3)

if else将强制编译器实例化两个控制流。由于存在T不是Numeric类型的情况(即使代码可能不通过该路径),这将导致编译错误。你需要的是编译时控制流程,在if_then_else

行中
template<int condition, int true_val, int false_val>
struct if_then_else {
    enum { val = true_val };
};

template<int true_val, int false_val>
struct if_then_else<false, true_val, false_val> {
    enum { val = false_val };
};

然后if_then_else< std::is_same<T, Numeric>::value, 1, 0>::value将为Numeric类型赋予1(true),为非数字类型赋予0(false),而无需无效地实例化非数字类型。

答案 3 :(得分:2)

MyClass<T>::checkNumeric()来电T::isNumeric()。您的String类没有这样的功能,因此MyClass<String>::checkNumeric()无法编译。

选项:

  • 添加并实施String::isNumeric()
  • 您已经从std::is_same收到了答案,那为何要致电isNumeric()

答案 4 :(得分:0)

有两种方法可以解决这个问题。

  1. addNumeric()添加到String。如果您有权修改String
  2. ,这是最简单的解决方案
  3. 如果您无权修改String,则可以使用帮助程序类来帮助完成此过程。
  4. 这是辅助类。

    template <typename T1> struct IsNumeric
    {
       static bool get(T1 const& temp)
       {
          return false;
       }
    };
    
    template <> struct IsNumeric<Numeric>
    {
       static bool get(Numeric const& temp)
       {
          return temp.isNumeric();
       }
    };
    

    这是你的主要课程:

    template <class T>
    class Myclass
    {
        private:
        T temp;
    
        public:
    
        void checkNumeric()
        {
           std::cout << " is numeric = " << IsNumeric<T>::get(temp) << std::endl;
        }
    };
    
相关问题