1
我有以下代碼:decltype和boost ::變種 - 檢索當前值
#include <boost/variant.hpp>
#include <iostream>
#include <string>
boost::variant<int, double, std::string> variant;
template <typename FirstArg, typename... OtherArgs>
auto bar(const std::type_info& variant_type_info) -> decltype(typeid(FirstArg) == variant_type_info ? boost::get<FirstArg>(variant) : bar<OtherArgs...>(variant_type_info))
{
if (typeid(FirstArg) == variant_type_info)
{
return boost::get<FirstArg>(variant);
}
return bar<OtherArgs...>(variant_type_info);
}
template <typename... VariantArgs>
auto foo(const boost::variant<VariantArgs...>& variant) -> decltype(bar<VariantArgs...>(variant.type()))
{
return bar<VariantArgs...>(variant.type());
}
int main()
{
variant = 0.5;
const auto& baz = foo(variant);
std::cout << baz << '\n';
}
它給我以下錯誤:
main.cpp:9:135: error: 'bar' was not declared in this scope
auto bar(const std::type_info& variant_type_info) -> decltype(typeid(FirstArg) == variant_type_info ? boost::get<FirstArg>(variant) : bar<OtherArgs...>(variant_type_info))
^
main.cpp:9:148: error: expected primary-expression before '...' token
auto bar(const std::type_info& variant_type_info) -> decltype(typeid(FirstArg) == variant_type_info ? boost::get<FirstArg>(variant) : bar<OtherArgs...>(variant_type_info))
^
main.cpp:9:148: error: expected ')' before '...' token
main.cpp:9:135: error: 'bar' was not declared in this scope
auto bar(const std::type_info& variant_type_info) -> decltype(typeid(FirstArg) == variant_type_info ? boost::get<FirstArg>(variant) : bar<OtherArgs...>(variant_type_info))
^
main.cpp:9:54: error: expected type-specifier before 'decltype'
auto bar(const std::type_info& variant_type_info) -> decltype(typeid(FirstArg) == variant_type_info ? boost::get<FirstArg>(variant) : bar<OtherArgs...>(variant_type_info))
^
main.cpp:9:54: error: expected initializer before 'decltype'
main.cpp:20:69: error: 'bar' was not declared in this scope
auto foo(const boost::variant<VariantArgs...>& variant) -> decltype(bar<VariantArgs...>(variant.type()))
^
main.cpp:20:69: error: 'bar' was not declared in this scope
main.cpp:20:84: error: expected primary-expression before '...' token
auto foo(const boost::variant<VariantArgs...>& variant) -> decltype(bar<VariantArgs...>(variant.type()))
^
main.cpp:20:84: error: expected ')' before '...' token
main.cpp:20:69: error: 'bar' was not declared in this scope
auto foo(const boost::variant<VariantArgs...>& variant) -> decltype(bar<VariantArgs...>(variant.type()))
^
main.cpp:20:69: error: 'bar' was not declared in this scope
main.cpp:20:60: error: expected type-specifier before 'decltype'
auto foo(const boost::variant<VariantArgs...>& variant) -> decltype(bar<VariantArgs...>(variant.type()))
^
main.cpp:20:60: error: expected initializer before 'decltype'
main.cpp: In function 'int main()':
main.cpp:28:34: error: 'foo' was not declared in this scope
const auto& baz = foo(variant);
Coliru例子 - http://coliru.stacked-crooked.com/a/4467c33489b08359
我見我不能在decltype中引用相同的函數。有沒有解決這種情況的解決方法?我正在嘗試編寫函數以從boost :: variant對象中檢索當前值。