从char数组到std :: string的类型推导

时间:2015-05-06 17:27:27

标签: c++ string variadic-templates

我正在尝试使用variadic模板编写sum函数。 在代码中我会写一些像

sum(1, 2., 3)

它将返回大多数一般类型的总和(在这种情况下为double)。 问题在于角色。当我称之为

sum("Hello", " ", "World!") 
例如,

模板参数推导为const char [7],因此无法编译。我找到了将最后一个参数指定为std::string("World!")的方法,但它并不漂亮,有没有办法实现std::string的自动类型推导或正确重载sum

到目前为止我的代码:

template<typename T1, typename T2>
auto sum(const T1& t1, const T2& t2) {
    return t1 + t2;
}

template<typename T1, typename... T2>
auto sum(const T1& t1, const T2&... t2) {
    return t1 + sum(t2...);
}

int main(int argc, char** argv) {
    auto s1 = sum(1, 2, 3);
    std::cout << typeid(s1).name() << " " << s1 << std::endl;

    auto s2 = sum(1, 2.0, 3);
    std::cout << typeid(s2).name() << " " << s2 << std::endl;

    auto s3 = sum("Hello", " ", std::string("World!"));
    std::cout << typeid(s3).name() << " " << s3 << std::endl;

    /* Won't compile! */
    /*
    auto s4 = sum("Hello", " ", "World!");
    std::cout << typeid(s4).name() << " " << s4 << std::endl;
    */

    return 0;
}

输出:

i 6
d 6
Ss Hello World!

2 个答案:

答案 0 :(得分:1)

最终,char指针类型与operator +的左侧右侧的处理方式不相符。“您可以超载如果需要,制造中介。

下面我自由地将其重构为单个param支持的sum(不是必需的步骤,但允许sum(x)重载,这更容易理解)。希望你明白了。

#include <iostream>
#include <string>

// generic identity
template<typename T1>
auto sum(const T1& t1)
{
    std::cout << __PRETTY_FUNCTION__ << '\n';
    return t1;
}

// specialized for const char [N]
auto sum(const char *s)
{
    std::cout << __PRETTY_FUNCTION__ << '\n';
    return std::string(s);
}

template<typename T1, typename... T2>
auto sum(const T1& t1, const T2&... t2)
{
    std::cout << __PRETTY_FUNCTION__ << '\n';
    return t1 + sum(t2...);
}

int main(int argc, char** argv) {

    std::cout << sum(1,2,3) << '\n';
    std::cout << sum(1, 2.0, 3) << '\n';
    std::cout << sum("Hello", " ", std::string("World!")) << '\n';
    std::cout << sum("Hello", " ", "World!");
    return 0;
}

<强>输出

auto sum(const T1 &, const T2 &...) [T1 = int, T2 = <int, int>]
auto sum(const T1 &, const T2 &...) [T1 = int, T2 = <int>]
auto sum(const T1 &) [T1 = int]
6
auto sum(const T1 &, const T2 &...) [T1 = int, T2 = <double, int>]
auto sum(const T1 &, const T2 &...) [T1 = double, T2 = <int>]
auto sum(const T1 &) [T1 = int]
6
auto sum(const T1 &, const T2 &...) [T1 = char [6], T2 = <char [2], std::__1::basic_string<char>>]
auto sum(const T1 &, const T2 &...) [T1 = char [2], T2 = <std::__1::basic_string<char>>]
auto sum(const T1 &) [T1 = std::__1::basic_string<char>]
Hello World!
auto sum(const T1 &, const T2 &...) [T1 = char [6], T2 = <char [2], char [7]>]
auto sum(const T1 &, const T2 &...) [T1 = char [2], T2 = <char [7]>]
auto sum(const char *)
Hello World!

祝你好运

答案 1 :(得分:1)

我只想编写一个简单的重载身份函数,专门处理const char*

template<typename T>
decltype(auto) fix(T&& val)
{
    return std::forward<T>(val);
} 

auto fix(char const* str)
{
    return std::string(str);
}

template<typename T1, typename T2>
auto sum(const T1& t1, const T2& t2) {
    return fix(t1) + t2;
}

template<typename T1, typename... T2>
auto sum(const T1& t1, const T2&... t2) {
    return t1 + sum(t2...);
}