decltype и boost::variant
От: FrozenHeart  
Дата: 10.02.14 21:19
Оценка:
Здравствуйте.

Имеется следующий код:

#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';
}


gcc 4.8 выдаёт на него следующие ошибки компиляции:

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);


Я понимаю, что дело в том, что я обращаюсь к той же самой функции в decltype. Существуют ли какие-нибудь workaround'ы на данную тему? Или можно ли как-то по-дргому решить задачу получения текущего значения объекта класса boost::variant?
avalon/1.0.433
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.