Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/131.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 模板功能:根据typename进行转换_C++_Oop_Templates - Fatal编程技术网

C++ 模板功能:根据typename进行转换

C++ 模板功能:根据typename进行转换,c++,oop,templates,C++,Oop,Templates,有没有一种方法可以在C++中实现类似的功能: template<typename T> T function() { std::string result = getRes(); // if (type == string) return result; // else if (type == numeric) return std::stoul(result, nullptr); } 模板 T函数() { std::stri

有没有一种方法可以在C++中实现类似的功能:

template<typename T>
T function()
{
    std::string result = getRes();
    // if (type == string)
        return result;
    // else if (type == numeric)
        return std::stoul(result, nullptr);
}
模板
T函数()
{
std::string result=getRes();
//如果(类型==字符串)
返回结果;
//else if(类型==数字)
返回std::stoul(结果,空ptr);
}
假设结果变量类型始终是已知的(在本例中为字符串)。返回类型由函数调用中的模板定义,例如:

int x = function<int>();
intx=function();
我知道有些东西像
std::is_integral
std::is_same
。使用这些函数,我可以确定T的类型。但是它似乎不起作用,因为它在编译时无法计算,因此编译器会抛出错误。

您可以使用:

#包括
模板
T函数()
{
std::string result=getRes();
细流ss;
ss>值;
返回值;
}

您可以使用SFINAE来区分函数模板的两个版本

#include <iostream>
#include <string>
#include <type_traits>

std::string getRes() { return "1729"; }

template < typename T, typename std::enable_if< std::is_same<T,std::string>::value, void** >::type = nullptr >
std::string function()
{
    return getRes();
}

template < typename T, typename std::enable_if< std::is_arithmetic<T>::value, void** >::type = nullptr >
unsigned long function()
{

    return std::stoul(getRes());
}

int main()
{
    unsigned long ul = function<unsigned long>();
    std::string s = function<std::string>();

    std::cout << ul << ' ' << s << '\n';
}

您可以使用
stringstream
。如果constexpr(blah){blah},您可以使用
,但是stringstream可能更好。stringstream解决方案似乎很有效,谢谢。你想写一个答案,这样我就可以接受它了吗?检查这是否适用于包含空格的字符串
result
。字符串提取在空白处停止,
#include <iostream>
#include <string>
#include <type_traits>

std::string getRes() { return "1729"; }

template < typename T, typename std::enable_if< std::is_same<T,std::string>::value, void** >::type = nullptr >
std::string function()
{
    return getRes();
}

template < typename T, typename std::enable_if< std::is_arithmetic<T>::value, void** >::type = nullptr >
unsigned long function()
{

    return std::stoul(getRes());
}

int main()
{
    unsigned long ul = function<unsigned long>();
    std::string s = function<std::string>();

    std::cout << ul << ' ' << s << '\n';
}
#include <iostream>
#include <string>
#include <type_traits>

std::string getRes() { return "1729"; }

template < typename T, std::enable_if_t< std::is_same<T,std::string>::value, void** > = nullptr >
auto function()
{
    return getRes();
}

template < typename T, std::enable_if_t< std::is_arithmetic<T>::value, void** > = nullptr >
auto function()
{

    return std::stoul(getRes());
}

int main()
{
    unsigned long ul = function<unsigned long>();
    std::string s = function<std::string>();

    std::cout << ul << ' ' << s << '\n';
}