Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to emulate 'const auto' with BOOST_AUTO in C++?

Tags:

c++

c++11

boost

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
like image 669
theV0ID Avatar asked Mar 17 '14 15:03

theV0ID


1 Answers

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

like image 134
sehe Avatar answered Nov 04 '22 09:11

sehe