如何效仿'常量汽车“在C ++ BOOST_AUTO? [英] How to emulate 'const auto' with BOOST_AUTO in C++?

查看:141
本文介绍了如何效仿'常量汽车“在C ++ BOOST_AUTO?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 BOOST_AUTO 宏,我们可以模拟汽车关键字,是不是以前C ++ 11可供选择:

  BOOST_AUTO(VAR,1 + 2); // INT VAR = 3
自动无功= 1 + 2; //在C ++ 11相同

有没有办法模仿常量汽车

 常量自动无功= 1 + 2; // const int的VAR = 3


解决方案

您可以只包括尾随常量:

 的#include<提升/ typeof运算/ typeof.hpp>诠释的main()
{
    BOOST_AUTO(常量的x,42);    static_assert(性病:: is_const< decltype(X)>(),weehoo);
}

尾随的位置是常量预选赛的原因是多方面的唯一的一贯立场。这就是其中之一:)

Using the BOOST_AUTO macro we can emulate the auto keyword that isn't available before C++11:

BOOST_AUTO( var, 1 + 2 ); // int var = 3
auto var = 1 + 2; // the same in C++11

Is there any way to emulate const auto?

const auto var = 1 + 2; // const int var = 3

解决方案

You can just include the "trailing" const:

#include <boost/typeof/typeof.hpp>

int main()
{
    BOOST_AUTO(const x, 42);

    static_assert(std::is_const<decltype(x)>(), "weehoo");
}

The trailing position is the only consistent position for the const qualifier for many reasons. This is one of them :)

这篇关于如何效仿'常量汽车“在C ++ BOOST_AUTO?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆