c++ - 如何使用 enable_if 和模板特化 c++?

标签 c++ templates template-specialization

我正在尝试在 boost 中使用 enable_if 来进行模板特化,但无法让它工作,并且对如何编写它以及语法的实际含义感到困惑。我已经阅读了 boost 文档,但对我来说仍然不太明白

我有四种方法,我希望它们都被命名为相同的东西并且都非常相似。我有两个将返回字符串的函数和两个将返回整数的函数。 在每种情况下,参数都是 int、int、返回类型或 int、const char*、返回类型,因为最后一个参数将是可以返回的默认值。

所以像这样...

int getVal(int,int,int)
int getVal(int,const char*,int)
string getVal(int,int,string)
string getVal(int,const char*,string)

但这行不通,因为参数相同,只有返回类型不同,我需要它是一个模板化方法,而不仅仅是一个重载方法。这是在非模板类中。所以如果返回类型是字符串或 int,我需要启用 enable_if,或者检查最后一个参数的 enable_if?

任何帮助将不胜感激,如果有人能够解释语法的含义并且实际上这样做也会有所帮助。谢谢

这是我尝试获得正确语法的众多尝试之一..

 template<typename myType> 
   typename enable_if<boost::is_arithmetic<myType>, myType>::type
    GetValue(int row, int col, myType d)
    { 
        unsigned int columnIndex = this->GetColumnIndex(col);

        try {
            CheckColumnType(col, Integer);
        }
        catch(DatatableException e){
            return d;
        }
        if("0" == this->m_rows[row][col])
        {
            return 0;
        }
        else if("1" == this->m_rows[row][col])
        {       
            return 1;
        }
        else
        {
            return atoi(this->m_rows[row][col].c_str());
        }
    }

template<typename myType> 
  typename disable_if<boost::is_arithmetic<myType>, myType>::type
   GetValue(int row, int col, myType d)
    {
        unsigned int columnIndex = this->GetColumnIndex(col);

        try {
            CheckColumnType(col, String);
        }
        catch(DatatableException e){            
            return d;

        }
        return this->m_rows[row][col];
    }

 template <class T>
  T GetValue(int row, typename enable_if<is_arithmetic<!T> >::type* dummy = 0){
{
            unsigned int columnIndex = this->GetColumnIndex(col);
            try {
                CheckColumnType(col, Integer);
            }
            catch(DatatableException e){
                return d;
            }
            if("0" == this->m_rows[row][col])
            {
                return 0;
            }
            else if("1" == this->m_rows[row][col])
            {       
                return 1;
            }
            else
            {
                return atoi(this->m_rows[row][col].c_str());
            }
    }

 template <class T>
    T GetValue(int row, typename enable_if<is_arithmetic<T> >::type* dummy = 0){

            try {
                CheckColumnType(col, Integer);
            }
            catch(DatatableException e){
                return d;
            }
            if("0" == this->m_rows[row][col])
            {
                return 0;
            }
            else if("1" == this->m_rows[row][col])
            {       
                return 1;
            }
            else
            {
                return atoi(this->m_rows[row][col].c_str());
            }
    }

最佳答案

我不确定 std::enable_if是你要找的。您是否尝试过使用函数模板特化?

template <typename T>
T function(int number, T const& result = T());

template <>
std::string function(int number, std::string const& result)
{
        // implementation for T = std::string
        return result;
}

template <>
int function(int number, int const& result)
{
        // implementation for T = int
        return result;
}

请注意,这需要 C++11。默认参数的歧义可以通过显式指定类型来解决,例如int ret = function<int>(1); .

关于c++ - 如何使用 enable_if 和模板特化 c++?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12934941/

相关文章:

c++ - 数据结构库供应商

c++ - 为什么必须在哪里放置 “template”和 “typename”关键字?

c++ - 模棱两可的模板函数特化编译错误

c++ - 类模板上的运算符重载

c++ - 为什么模板只能在头文件中实现?

c++ - 在类模板中找不到声明的类型

c++ - 从基非模板类调用派生模板类方法

c++ - 递归变量容器编译器错误

c++ - 文件读取类C++

c++ - linux c++串口回显输出