C++ 条件执行取决于类型

fol*_*bis 3 c++ templates c++11

我有一个模板函数,应该根据类型执行不同的代码。简化后的函数如下所示:

template<typename T>
std::string test(T value)
{
    std::string v;
    if(std::is_arithmetic<T>())
    {
        v = std::to_string(value);
    }
    else
    {
       v = std::string(value);
    }
    return v;
}
Run Code Online (Sandbox Code Playgroud)

用法:

test("Hello");
test(123);
Run Code Online (Sandbox Code Playgroud)

但我收到这个错误:

In instantiation of void test(T) [with T = const char*]:
error: no matching function for call to to_string(const char*)
note: candidate: std::string std::__cxx11::to_string(int) <near match>
to_string(int __val)

and the same for the following:

to_string(unsigned __val)
to_string(long __val)
to_string(unsigned long __val)
Run Code Online (Sandbox Code Playgroud)

好的,我明白,在这种情况下,const char *编译将失败,因为没有std::to_string(const char *). 但我怎样才能让代码起作用呢?只需要注意,在我的真实代码中我限制为c++11.

Sto*_*ica 5

您现在可以了解为什么if constexpr要添加到该语言中。如果您需要执行一些依赖于类型的操作作为较大算法的一部分,那么在 C++17 之前的版本中,通常通过tag-dispatch来执行此操作

namespace detail {
    template<typename T>
    std::string stringify(T value, std::true_type) {
        return std::to_string(value);
    }
    template<typename T>
    std::string stringify(T value, std::false_type) {
        return std::string(value);
    }
}

template<typename T>
std::string test(T value)
{
    std::string v;
    v = detail::stringify(value, std::is_arithmetic<T>());
    return v;
}
Run Code Online (Sandbox Code Playgroud)

这是在两个条件下进行调度,但该技术可以扩展到多个重载,具体取决于您构建标记类型的方式。标准中的一个常见示例是迭代器类别