2
我做函数转换整数或浮弦:如果constexpr和语法错误“类型”
#include <functional>
#include <string>
#include <iostream>
template <typename From>
inline std::string ToStr(const From& src)
{
constexpr bool isIntegral = std::is_integral<From>::value;
constexpr bool isFloat = std::is_floating_point<From>::value;
if constexpr (isIntegral)
return (std::_Integral_to_string<char>(src));
else if (isFloat)
return (std::_Floating_to_string("%f", src));
}
int main()
{
std::cout << ToStr(123);
}
我使用Visual Studio 2017年,我得到以下错误:
- 语法错误“ (第一if)
- 非法否则不匹配如果在线13(第二if)
我不知道如何解决它,任何想法?
第四行没有'type'。出示您的[MCVE]。 –
高度相关:https://stackoverflow.com/questions/5246049/c11-static-assert-and-template-instantiation – Rakete1111
@LightnessRacesinOrbit但是完全相同的代码给出了这个错误。我很困惑,因为你写的那条线上没有'type'。 – Condzi