如何在C++中模拟带有BOOST_AUTO的"const auto"?
How to emulate 'const auto' with BOOST_AUTO in C++?
使用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
限定符唯一一致的位置。这是其中之一
相关文章:
- 为什么当我为 for(auto& it : myUnorderedMap) {... = std::move(it.second)} 时,我会得到一个 const 引用?
- 为什么 const auto &p{nullptr} 在 C++17 中不起作用,而 auto *p{nullptr} 不起作用?
- const auto & 和 auto & if reference 对象之间的区别是 const
- 为什么"const auto [x, y]"绑定到引用类型时没有按预期运行?
- 用 decltype 和 auto 推导出 const(返回)类型
- 避免使用 auto 关键字从字面上复制 const 和非 const 的代码?
- 将右值分配给'const auto&'时会发生什么情况
- 为什么在auto和template函数的情况下,类型都没有推导为"const"类型
- auto&x = const int *的类型是什么?
- 将 const auto & 转换为迭代器
- 返回指向 const 数据成员和 'auto' 关键字的 const 指针。有点困惑
- 为什么"auto"将字符串声明为 const char* 而不是 std::string?
- 'const auto'的间接级别与'const char *const '不同
- 如何在STLport库中使用const auto
- auto and const object
- 在 for-each 循环中使用 'auto' 时是否需要添加'const'?
- C++ : const int * 和 cont int & 之间的 auto 类型不同的扣除
- 返回const auto对象——和Qt隐式共享
- const auto std::initializer_list Clang与GCC的差异
- const auto会导致编译器崩溃