草庐IT

compile-time-constant

全部标签

c++ - 具有模板特化的 constexpr 数组成员 : inconsistent behavior cross compilers

考虑以下代码:#includetemplatestructfoo{};templatestructfoo{staticconstexprcharvalue[]="abcde";};templatestructbar{staticconstexprcharvalue[]="abcde";};templatestructbaz{staticconstexprintvalue=12345;};intmain(){charc=foo::value[2];chard=bar::value[2];inte=baz::value;std::cout编译时:clang++-std=c++14./tes

c++ - 为什么 std::is_copy_constructible 的行为不如预期?

#includeintmain(){std::is_constructible_v;//false,asexpected.std::is_copy_constructible_v;//true,NOTasexpected!}根据cppref:IfTisanobjectorreferencetypeandthevariabledefinitionTobj(std::declval()...);iswell-formed,providesthememberconstantvalueequaltotrue.Inallothercases,valueisfalse.std::is_copy_c

c++ - 为什么 std::chrono::system_clock::to_time_t() 不是 constexpr?

C++标准(github.com/cplusplus/draft)有time_t转换函数(std::chrono::system_clock::to_time_t和std::chrono::system_clock::from_time_t)用于列为static和noexcept但不是constexpr。鉴于time_point和duration上的所有操作基本上都是constexpr(包括duration_cast和time_point_cast),我想不出任何理由来排除它们。在我的本地机器上快速检查libstdc++源代码确认这些函数是作为简单的持续时间/时间点转换实现的。这两个函

c++ - std::integral_constant<T, v>::value 总是有定义吗?

在C++14标准中,std::integral_constant模板定义如下:templatestructintegral_constant{staticconstexprTvalue=v;typedefTvalue_type;typedefintegral_constanttype;constexproperatorvalue_type()constnoexcept{returnvalue;}constexprvalue_typeoperator()()constnoexcept{returnvalue;}};它没有说明静态数据成员是否有相应的外联定义,即,templateconst

c++ - 与 friend 一起上课而不是前向声明,: which compiler is correct

这个问题在这里已经有了答案:Friendmethod"notdeclaredinthisscope"inC++(1个回答)Error:'FriendMemberFunctionName'wasnotdeclaredinthisscope(3个答案)关闭3年前。我有这个简单的C++程序:#includestructobj{friendintf(int);voidm(intx){std::cout如果我使用GNUC++编译器g++进行编译,我会得到错误prog.cpp:7:55:error:'f'wasnotdeclaredinthisscope但是,如果我使用cl(和/W4)编译它,它会

c++ - G++ 4.6 -std=gnu++0x : Static Local Variable Constructor Call Timing and Thread Safety

voida(){...}voidb(){...}structX{X(){b();}};voidf(){a();staticXx;...}假设在进入main之后,f被多个线程(可能竞争)多次调用。(当然,唯一对a和b的调用是上面看到的那些)以上代码在-std=gnu++0x模式下用gccg++4.6编译时:Q1。是否保证至少调用一次a()并在调用b()之前返回?也就是说,在第一次调用f()时,x的构造函数是否会同时调用一个自动持续时间局部变量(非静态)(而不是在全局静态初始化时间)?Q2。是否保证b()只会被调用一次?即使两个线程第一次同时在不同的核上执行f?如果是,GCC生成的代码通过

c++ - 如何将大量秒数可移植地添加到 time_t 对象?

嗯,我认为标题总结了它。假设我有一个double类型的对象,它是通过在两个time_t对象上运行std::difftime获得的,现在我想添加结果返回到time_t对象的秒数。我不介意损失几分之一秒。请注意,秒数可能很大(即大于structtm中允许的60秒,但始终低于用于在相应机器/实现上表示秒的任何整数原语,并且永远不会超过1年的数量级,尽管我最好不希望这是一个限制)。我将如何着手可移植(即按照C标准)?我希望不必将它们分为月、日、小时、分钟等,然后手动将它们添加到structtm对象中。当然有更好的方法!? 最佳答案 您可以使

c++ - 编译时模板 `std::integral_constant` 计数器 - 如何实现它?

我有几种类型,我想“绑定(bind)”一个std::integral_constant编译时每种类型的顺序ID值。例子:structType00{};structType01{};structType02{};structType03{};structTypeXX{};structTypeYY{};templatestructTypeInfo{usingId=std::integral_constant;};intmain(){cout::Id::value;//Shouldalwaysprint0cout::Id::value;//Shouldalwaysprint1cout::Id

c++ - STL 容器 move 语义并按值返回 : how many times of copying get avoided away?

我知道在C++11中,move语义已经在STL容器中实现以避免临时对象。人们说现在编写按值返回的函数是完美的。但我对究竟有多少次复制实际上被避免感到困惑。请看下面的例子:vectormyVector(){vectorres;res.push_back(4);res.push_back(5);returnres;}vectorv=myVector();我的理解是在c++03中,myVector返回res的拷贝(4,5复制了一次),在评估vectorv=myVector();时vector的复制构造函数vector(constvector&)被调用(4,5复制了两次)。但是在具有move语

c++ - 尝试使用 std::get_time 解析 YYMMDD 并失败

我正在尝试这样做但失败了:std::istringstreamss("1212");ss>>std::get_time(&t,"%y%m");if(ss.fail())//everytime!这工作正常:std::istringstreamss("12-12");ss>>std::get_time(&t,"%y-%m");知道我做错了什么吗?我还能用什么,因为Windows似乎没有srtptimewindow/vs13时间差 最佳答案 VisualStudio似乎没有正确实现规范,GCCuntilversion5.0也没有。.如果您