如何在C++中模拟带有BOOST_AUTO的"const auto"?

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

本文关键字:AUTO const auto BOOST C++ 模拟      更新时间:2023-10-16

使用BOOST_AUTO宏,我们可以模拟在c++ 11之前不可用的auto关键字:

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

是否有办法模拟const auto ?

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

你可以只包含"尾" const:

#include <boost/typeof/typeof.hpp>
int main()
{
    BOOST_AUTO(const x, 42);
    static_assert(std::is_const<decltype(x)>(), "weehoo");
}

由于许多原因,末尾位置是const限定符唯一一致的位置。这是其中之一