How to emulate 'const auto' with BOOST_AUT

2019-06-18 16:02发布

问题:

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

回答1:

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++ c++11 boost