为什么std::is_same需要双括号
Why are double parentheses needed with std::is_same
我试图使用std::is_same
来验证强类型枚举的底层类型,我注意到一个奇怪的情况,我需要使用双括号,但我不明白为什么。我把这个例子简化为:
#include <type_traits>
#include <cassert>
#include <stdint.h>
int main(int argc, char *argv[])
{
assert((std::is_same<unsigned int,uint32_t>::value == true)); // OK
assert((std::is_same<unsigned int,uint32_t>::value) == true); // OK
//assert(std::is_same<unsigned int,uint32_t>::value == true); // Compile error
static_assert(std::is_same<unsigned int,uint32_t>::value == true, "BAD"); // OK
return 0;
}
编译错误:
isSameAssert.cpp:9:62: error: macro "assert" passed 2 arguments, but takes just 1
assert(std::is_same<unsigned int,uint32_t>::value == true); // Compile error
^
isSameAssert.cpp: In function ‘int main(int, char**)’:
isSameAssert.cpp:9:5: error: ‘assert’ was not declared in this scope
assert(std::is_same<unsigned int,uint32_t>::value == true); // Compile error
^
make: *** [build/isSameAssert] Error 1
有人能解释一下吗?或者给我指一个可以解释的参考?
因为assert
是一个宏,表达式assert(std::is_same<unsigned int,uint32_t>::value == true);
似乎用两个参数调用assert
,因为int
和uint32_t
之间有逗号,所以编译器抱怨assert
只接受一个参数,但提供了两个参数。
事实上,把它再次放在括号里解决了这个问题。
相关文章:
- 使用std::multimap迭代器创建std::list
- C++中std::resize(n)和std::shrink_to_fit之间的区别
- 来自 std::list 的迭代器 .end() 按预期返回"0xcdcdcdcdcdcdcdcd"但 .begin()
- C++17复制构造函数,在std::unordereded_map上进行深度复制
- 如何导出包含具有"std::unique_ptr"值的"std::map"属性的
- 从持续时间构造std::chrono::system_clock::time_point
- std::具有相同基类的类的变体
- std::向量与传递值的动态数组
- 使用std::vector的OpenCL矩阵乘法
- std::map<struct,struct>::find 找不到匹配项,但是如果我循环通过 begin() 到 end(),我在那里看到匹配项
- std::condition_variable::wait()如何评估给定的谓词
- 如何获取std::result_of函数的返回类型
- std::原子加载和存储都需要吗
- 将对象移动到std::shared_ptr
- POCO::PostgreSQL:如何将std::vector支持添加到`Binder::bind`
- 使用一个考虑到std::map中键值的滚动或换行的键
- 如何从 std::atomic 中提取指针 T<T>?
- 为什么 std::unique 不调用 std::sort?
- 使用std::函数映射对象方法
- 为什么 std::shared_ptr 被认为是"heavy"和"expensive",但 std::array "same perfprmance as plain (c-style) arrays