c++ - 基于类型的任意属性的函数重载不起作用

标签 c++ generic-programming sfinae

在下面的示例中,我需要提取一些值。我有一个高效的提取器,可以使用内置类型,还有一个低效的模板,可以处理所有内容。要在这些之间进行选择,我想使用 Function Overloading Based on Arbitrary Properties of Types 。这是我的代码:

#include <string>
#include <iostream>

class extractor
{
public:
    static void extract(const bool& val) { std::cout << "Specialized extractor called" << std::endl; }
    static void extract(const double& val) { std::cout << "Specialized extractor called" << std::endl; }
};

template <typename T>
void extract_generic(const T& value) { std::cout << "Generic extractor called" << std::endl; }


template <typename T> struct is_extractor_native { static const bool val = false; };
template<> struct is_extractor_native<bool> {static const bool val = true; };
template<> struct is_extractor_native<double>  {static const bool val = true; };


template <bool B, class T = void>
struct enable_if {
    typedef T type;
};

template <class T>
struct enable_if<false, T> {};

template <typename T>
struct extract_caller
{

    //line 32 below
    static void extract(const T& val, typename enable_if<is_extractor_native<T>::val>::type * = 0)
    {
            extractor::extract(val);
    }

    //line 37 below
    static void extract(const T& val, typename enable_if<!is_extractor_native<T>::val>::type * = 0)
    {
            extract_generic(val);
    }
};



int main(void)
{
    std::string string_value("hello");
    double double_value(.123);

    std::cout << "native extractor for std::string: " << (int)is_extractor_native<std::string>::val << std::endl;
    std::cout << "native extractor for double:      " << (int)is_extractor_native<double>::val << std::endl;

    extract_caller<std::string>::extract(string_value);
    extract_caller<double>::extract(double_value);

    return 0;
}    

当我构建编译器时提示:

g++     main.cpp   -o main
main.cpp: In instantiation of ‘extract_caller<std::basic_string<char> >’:
main.cpp:50:29:   instantiated from here
main.cpp:32:14: error: no type named ‘type’ in ‘struct enable_if<false, void>’
main.cpp: In instantiation of ‘extract_caller<double>’:
main.cpp:51:24:   instantiated from here
main.cpp:37:14: error: no type named ‘type’ in ‘struct enable_if<false, void>’
make: *** [main] Error 1

当注释掉提取并仅打印特征时,我得到正确的结果:

./main
native extractor for std::string: 0
native extractor for double:      1

在错误列表中,您可以看到对于 double,编译器在第 32 行传递原型(prototype),转到第 37 行并打印错误。问题是,为什么这里不应用SFINAE原则?

最佳答案

SFINAE 仅在声明部分发生某些错误(例如您所遇到的错误)时才起作用。在你的情况下,它们发生在定义中。您必须重写代码,以便在声明中使用 enable_if(在 struct extract_caller 的情况下)。

关于c++ - 基于类型的任意属性的函数重载不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9556924/

相关文章:

c# - 用于组织的 Linq 表达式(类似于 Group By 但不同)

c++ - 如何专门化基于 bool 元函数的类模板?

c++ - 基于依赖类型的存在重载

c++ - 轻松编写保持状态的模拟

c++ - 类型特征以获取 std::array 或 C 风格数组的元素类型

java - 二元运算符的操作数类型错误 '+'

c++ - 转换字符串标记流时如何避免重复istringstream构造

c++ - 使用 SFINAE 在 C++ 中强制类型转换

c++ - 如何在C++中使用MySQL的STR_TO_DATE?

c++ - 返回唯一指针的 STL 容器