Lig*_*ica 5 c++ visual-studio c++17 visual-studio-2017
这是一些代码:
#include <iostream>
#include <functional>
#include <string>
#include <variant>
namespace detail
{
std::string Foo(const double val)
{
return std::to_string(val);
}
}
using Value = std::variant<std::monostate, double, int, bool, std::string>;
struct ReadStore {};
struct ValueException : std::runtime_error {};
using PrintConvType = std::function<Value(const Value&, ReadStore&)>;
template <typename InType, typename OutType, typename CallableType, CallableType Callable, bool Strict>
Value ConvWrapperImpl(const Value& value)
{
if constexpr (std::is_same_v<InType, Value>)
{
return Callable(value);
}
else
{
if (value.index() != 0)
{
const auto* ptr = std::get_if<InType>(&value);
if (ptr)
return Callable(*ptr);
}
return value;
}
}
template <typename T1, typename T2, T2 (*Callable)(const T1&), bool Strict = false>
Value ConvWrapper(const Value& value, ReadStore&)
{
return ConvWrapperImpl<T1, T2, decltype(Callable), Callable, Strict>(value);
}
template <typename T1, typename T2, T2 (*Callable)(T1), bool Strict = false>
Value ConvWrapper(const Value& value, ReadStore&)
{
return ConvWrapperImpl<T1, T2, decltype(Callable), Callable, Strict>(value);
}
int main()
{
using namespace detail;
ReadStore store;
PrintConvType func = ConvWrapper<double, std::string, Foo>;
Value result = func(3.14159, store);
std::cout << std::get<std::string>(result) << '\n';
}
Run Code Online (Sandbox Code Playgroud)
它是人为设计的MCVE,但原始项目中的总体思路是提供一种简化方法,用于将转换Value为某些回调函数的适当参数类型包装,并将该函数的返回类型的转换Value再次包装回。
这在GCC和Clang 下很好,但是我的Visual Studio 2017(v15.7.2)错误:
error C2440: 'specialization': cannot convert from 'std::string (__cdecl *)(const double)' to 'std::string (__cdecl *)(const double &)'
note: This conversion requires a reinterpret_cast, a C-style cast or a function-style cast
error C2973: 'convWrapper': invalid template argument 'std::string (__cdecl *)(const double)'
note: see declaration of 'ConvWrapper'
Run Code Online (Sandbox Code Playgroud)
根据最后一个音符的链接位置,似乎选择了错误,ConvWrapper然后在Callable签名不匹配时表现出惊讶。
奇怪的是,在选择了相同编译器的Godbolt上,代码被接受了。
是否有可能影响此配置?那是我吗?
我该如何解决?更改配置还是更改代码?
导致问题的编译器标志是/permissive-一致性标志。从编译器命令中删除它,或者在“调试”->“项目名称”属性-> C / C ++->“语言”->“一致性模式”中将其更改为“否”,即可对其进行编译。