如您所见,我正在使用一些模板技巧来推断某些函数的返回类型,但由于某种原因,此代码无法编译。我希望您帮助了解错误的原因以及如果可能的话如何解决它。
变体类可以在这里找到。
我有以下代码:
class Value {
....
using values = variant<std::shared_ptr<const std::string> , unsigned int, float, bool>;
values _holder;
template<typename To> using ResolveReturnConvertType = decltype(mapbox::util::apply_visitor(typename ConverterAdaptor<To>::ConverterType(), _holder));
template<typename To>
auto convertTo() const noexcept -> ResolveReturnConvertType<To>
{
static_assert(std::is_constructible<Value, To>::value, "Type requested to convert is not valid");
using ReturnType = ResolveReturnConvertType<To>; //deduces ReturnType = int in this case, that type is valid
_holder.get<ReturnType>() // fails to compile, error below
std::cout << typeid(ReturnType).name() << std::endl; //works why?
return mapbox::util::apply_visitor(typename ConverterAdaptor<To>::ConverterType(), _holder);
}
}//end of Value
//example main
int main() {
Value v("232");
std::cout << v.convertTo<int>() << std::endl;
}
编译器错误
g++ -std=c++11 -Ideps -I. -c -o Value/Value.o Value/Value.cpp
In file included from Value/Value.cpp:1:0:
Value/Value.hpp: In instantiation of ‘Value::ResolveReturnConvertType<To> Value::convertTo() const [with To = int; Value::ResolveReturnConvertType<To> = int; typename ConverterAdaptor<To>::ConverterType = NumericConverter<int>]’:
Value/Value.cpp:27:33: required from here
Value/Value.hpp:88:3: error: no matching function for call to ‘mapbox::util::variant<std::shared_ptr<const std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, unsigned int, float, bool, std::shared_ptr<const std::vector<std::shared_ptr<const Value>, std::allocator<std::shared_ptr<const Value> > > >, std::shared_ptr<const std::unordered_map<std::shared_ptr<const std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::shared_ptr<const Value>, std::hash<std::shared_ptr<const std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >, std::equal_to<std::shared_ptr<const std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >, std::allocator<std::pair<const std::shared_ptr<const std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::shared_ptr<const Value> > > > > >::get() const’
_holder.get<ReturnType>();
^
In file included from Value/Value.hpp:8:0,
from Value/Value.cpp:1:
deps/variant/variant.hpp:680:23: note: candidate: template<class T, typename std::enable_if<(mapbox::util::detail::direct_type<T, std::shared_ptr<const std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, unsigned int, float, bool, std::shared_ptr<const std::vector<std::shared_ptr<const Value>, std::allocator<std::shared_ptr<const Value> > > >, std::shared_ptr<const std::unordered_map<std::shared_ptr<const std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::shared_ptr<const Value>, std::hash<std::shared_ptr<const std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >, std::equal_to<std::shared_ptr<const std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >, std::allocator<std::pair<const std::shared_ptr<const std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::shared_ptr<const Value> > > > > >::index != mapbox::util::detail::invalid_value), void>::type* <anonymous> > T& mapbox::util::variant<Types>::get() [with T = T; typename std::enable_if<(mapbox::util::detail::direct_type<T, Types ...>::index != mapbox::util::detail::invalid_value)>::type* <anonymous> = <enumerator>; Types = {std::shared_ptr<const std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, unsigned int, float, bool, std::shared_ptr<const std::vector<std::shared_ptr<const Value>, std::allocator<std::shared_ptr<const Value> > > >, std::shared_ptr<const std::unordered_map<std::shared_ptr<const std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::shared_ptr<const Value>, std::hash<std::shared_ptr<const std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >, std::equal_to<std::shared_ptr<const std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >, std::allocator<std::pair<const std::shared_ptr<const std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::shared_ptr<const Value> > > > >}]
VARIANT_INLINE T& get()
这个问题
可以关闭,这个问题是由于我的一个愚蠢的错误,int实际上不是一个有效的类型,因此模板推导失败了。