草庐IT

assert_difference

全部标签

c++ - 部分模板特化触发 static_asserts

考虑这段代码templatestructdelay:std::false_type{};templatestructmy_typelist{static_assert(delay{},"");};templatestructtest;templatestructtest>{voidpass(){}};templatevoidfail(consttest&){}intmain(){test>t;t.pass();fail(t);}不调用fail()代码编译并运行良好。但是,在任何函数中使用t似乎都会触发my_typelist类中的static_assert,即使该类从未被实例化。尽管该示

c++ - 杀死魔数(Magic Number): "const int" vs "constexpr int" (or is there no difference in the end)

假设我有一个魔数(MagicNumber)我想摆脱...//whatever.cppfor(inti=0;i我可以通过两种方式杀死它:无论是constintSOMETHING_SOMETHING_MEANING_OF_LIFE=42或使用constexprintSOMETHING_SOMETHING_MEANING_OF_LIFE=42在源.cpp文件中。在这种情况下,两者之间是否有任何有意义的区别(我记得编译器推断-在任何一种情况下-值都不会改变,因此42实际上在结果循环中是硬编码的/展开循环/任何机器代码)还是归结为个人喜好?在一个相关问题中:如果magicnumber(以及替换它

带有 enable_if : different behaviour with g++ and clang 的 C++ 模板重载

在解决基类的模板化成员函数的重载时,我观察到g++(5.2.1-23)和clang(3.8.0)之间的不同行为,-std=c++14.#include#includestructBase{templateautoa(Tt)->void{std::coutstructDerived:publicBase{usingBase::a;templateautoa(Tt)->std::enable_if_t{std::coutd;d.a(1);//failswithg++,prints"true"withclangDerivedd2;d2.a(1);//failswithclang++,prin

C++,OpenCV : Assertion failed in Resize

作为一名C++初学者,我目前面临一个我无法解决的问题,即使代码非常简单。我一直在寻找answers整个互联网,但没有一个适用于我的问题。我目前正在使用OpenCV2.4.8在VS2013下使用C++编写基本SVM。我能够处理相同大小的图像,在我的代码开头指定固定的高度、宽度。现在,我正在尝试:打开不同大小的图像,将它们调整为某个较小的大小,并将之前的代码应用于现在调整大小的数据集。就这么简单。这是我的代码的开头:#include#include#include#include#include#include#include#include#include#includeusingnam

c++ - static_assert 内部/外部类定义

为什么static_assert需要在类定义之外?失败代码#includeclassA{public:A(A&&)noexcept{}static_assert(std::is_nothrow_move_constructible::value,"ERROR");};intmain(){}工作代码#includeclassA{public:A(A&&)noexcept{}};static_assert(std::is_nothrow_move_constructible::value,"ERROR");intmain(){}什么时候适合在类或结构的定义中使用static_asserts

c++ - 编译时等效的 std::cout,或 c++11 中编译时常量值的 static_assert 字符串化

有没有办法打印constexpr的值?或#defined编译时的值?我想要std::cout的等价物,或某种方式来做类似的事情constexprintPI_INT=4;static_assert(PI_INT==3,const_str_join("PI_INTmustbe3,not",const_int_to_str(PI_INT)));编辑:我可以使用constexpr进行一些基本的编译时打印s,至少在gcc上做类似的事情templatestructdisplay_non_zero_int_value;templatestructdisplay_non_zero_int_value{

c++ - MSVC++ : template's static_assert is not triggered inside a lambda

更新2:这已在VS2019Preview16.1Preview1中得到修复。更新:我已在visualstudio.com提交错误报告.所以我开始研究C++的模板,当我试图阻止使用static_assert编译模板类时遇到了这个问题。基本上,static_assert错误在VS2017上使用C++语言标准:ISOC++17标准(/std:c++17)。我也在gcc-7上使用-std=c++17进行了尝试,并触发了错误。这是VS2017上的错误还是我遗漏了什么?代码示例:#include#include#includetemplateclassIntegralContainer{stati

C++ 断言 : the precedence of the expression in an assert macro

在C++中:assert(std::is_same::value);//doesnotcompileassert((std::is_same::value));//compiles谁能解释一下原因? 最佳答案 assert是一个预处理器宏。预处理器宏是愚蠢的;他们不懂模板。预处理器在括号内看到10个标记:assert(std::is_same::value);它以逗号分隔。它不知道这是错误的分割位置,因为它不明白std::is_same和int>::value不是有效的C++表达式。预处理器足够聪明,不会在多个参数之间分解内部括号对

c++ - 构造函数 : difference between defaulting and delegating a parameter

今天,我偶然发现了thesestandarddeclarationsstd::vector构造函数://untilC++14explicitvector(constAllocator&alloc=Allocator());//sinceC++14vector():vector(Allocator()){}explicitvector(constAllocator&alloc);这种变化可以在大多数标准容器中看到。一个稍微不同的例子是std::set://untilC++14explicitset(constCompare&comp=Compare(),constAllocator&al

c++ - 我误解了 assert() 的用法吗?

我在看theassert()referencepage当我阅读给定的示例时,我被卡住了:/*assertexample*/#include#includeintmain(){FILE*datafile;datafile=fopen("file.dat","r");assert(datafile);fclose(datafile);return0;}Inthisexample,assertisusedtoaborttheprogramexecutionifdatafilecomparesequalto0,whichhappenswhenthepreviouscalltofopenwasn