首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >解密和升压::变量-检索当前值

解密和升压::变量-检索当前值
EN

Stack Overflow用户
提问于 2014-02-10 20:53:25
回答 1查看 1.6K关注 0票数 2

我有以下代码:

代码语言:javascript
复制
#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';
}

它给我带来了以下错误:

代码语言:javascript
复制
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

我发现我不能在解密中引用相同的函数。这种情况有什么解决办法吗?我试图编写函数从boost::variant对象中检索当前值。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-02-10 21:52:57

是的,在一般情况下有一个解决办法。它叫:

偏序要获得前向引用,请将方法移到类命名空间中。

然而,

这里什么也救不了你。除非您在编译时已经知道了参数的静态类型,否则不可能希望编译器恢复参数的静态类型。这应该是显而易见的。

我的意思是,foo()的返回类型应该是什么?它怎么可能包含所有可能的元素类型?正确的。这就是为什么你有变体的原因。

前进的三条道路:

  1. 静态地了解类型: const & baz =boost::get(变体);
  2. 返回类型为: std::type_info const& typeinfo_value = variant.type();
  3. 分支并创建单独的路径来处理所有情况。Boost为本例提供了static_visitor: 结构访问者: boost::static_visitor { std::string运算符()( std::string const&) const {返回" std::string ";}std::string运算符(Int)(Int) const {int "int";}std::string运算符()()(Double) const {返回"double";} template std::string运算符()(boost::variant const& v) const {返回boost::apply_visitor(*this,v);};int main() {静态const访问者_visitor;变量= 0.5f;std::cout <<“实际类型为”<< _visitor(变体) << "\n";}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21687663

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档