草庐IT

brace-init

全部标签

c++ - iterator_traits<InIter>::value_type 的创建是否会在传递时触发遵从? (异常测试)

我在故意为不特别遵守迭代器的函数抛出异常(出于测试目的)时遇到了麻烦。要了解我在做什么,请带上我的decorator_iterator结构:structdecorated_iterator:boost::iterator_adaptor,BaseIterator,boost::use_default,IteratorTag>{//....private:friendclassboost::iterator_core_access;/*usedtothrowanexceptionupondereference*/typenamebase_type::referencedereferenc

c++ - 成员初始化列表符号 : curly braces vs parentheses

考虑pg中的以下代码片段。17的C++之旅:classVector{public:Vector(ints):elem{newdouble[s]},sz{s}{}//constructaVectordouble&operator[](inti){returnelem[i];}//elementaccess:subscriptingintsize(){returnsz;}private:double*elem;//pointertotheelementsintsz;//thenumberofelements};这里我关心的是第三行的成员初始化列表,其中Stroustrup将冒号与两个初始化

c++ - (Swig to python)导入错误:dynamic module does not define init function

我正在尝试通过swig将我的C++代码移植到Python。当我完成py、pyd、cxx和lib文件的构建时,在Python(命令行)下,我键入“模块Dnld”,它显示->导入错误:动态模块未定义初始化函数。以下是我的代码,进一步:添加我的构建步骤以避免误解,谢谢MarkTolonen文件->新建->项目->Windows控制台应用程序->选择DLL和空项目(无unicode)将我的SerialComm文件夹添加到项目中(包括DownloaderEngine.hSerial.hPortEnumerator.h等)。配置属性->c/c++->附加包含目录->C:\Python27\incl

c++ - GCC 中的 __attribute__((init_priority(X)))

我在GCC中使用__attribute__((init_priority(X)))是这样的:Type1__attribute__((init_priority(101)))name1=value1;Type2__attribute__((init_priority(102)))name2=value2;在不同的源文件中。比方说file1.cpp和file2.cpp。如果我在同一个库中使用它,它会按预期工作,name1在name2之前初始化,但如果我在不同的库中使用它,则初始化顺序不是预期的顺序。我在gcc文档中读到这应该像我期望的那样在不同的库中工作,以定义初始化的顺序。我使用它的方式

c++ - 什么时候允许编译器优化 auto+brace 样式初始化?

假设您有一个名为Product的类,定义如下:classProduct{public:Product(constchar*name,inti);Product(Product&&rhs);Product(constProduct&rhs);~Product();private:constchar*m_name;intm_i;};然后你像这样初始化一个变量:autop=Product{"abc",123};我认为标准规定编译器必须在逻辑上执行以下操作:构建一个临时产品移动构建p(使用临时Product)但是允许编译器对其进行优化,以便直接构造p。我验证了这一点(VisualStudio2

c++ - constexpr(gcc) 错误 - 错误 : a brace-enclosed initializer is not allowed here before '{' token

structX{constexprstaticchara1[]="hello";//Okayconstexprstaticconstchar*a2[]={"hello"};//Error};intmain(){}用gcc编译报错:error:abrace-enclosedinitializerisnotallowedherebefore'{'token这是对constexpr的非法使用吗?编辑我尝试了3个不同版本的gcc,它是在我拥有的最新4.7.0上编译的(我刚刚下载了它,我使用的是mingw-w64),所以它看起来是一个固定的错误(链接到bug会很好!)。4.7.020120311

c++ - Microsoft 的 GCC 选项 init_priority 是什么?

在编译和链接C++库或程序时,无法保证翻译单元之间静态C++对象的初始化顺序。GCCoffersinit_priority解决静态存档、共享对象和程序的问题:init_priority(priority)    InStandardC++,objectsdefinedatnamespacescopeareguaranteedto    beinitializedinanorderinstrictaccordancewiththatoftheir    definitionsinagiventranslationunit.Noguaranteeismadefor    initializ

c++ - 在 init 方法中绕过 const

所以我can'tuseinitializersinmyclassconstructor因为使用数组,所以我决定改用init()方法。现在我有一个不同的问题。我有这样一个类:classEPWM{private:volatileEPWM_REGS*constregs;public:voidinit(volatileEPWM_REGS*_regs);};我需要通过初始化regs=_regs;来实现init()但我不能因为const。有没有办法在我的init方法中强制分配?我想保留const关键字,这样我就不会不小心重新分配到别处。编辑:尽管我很想使用构造函数+初始值设定项来解决这个问题(我的

c++ - Braced-init-lists 和函数模板类型推导顺序

我有一个关于函数模板参数类型推导过程的问题。举个例子:#include#include#include#include#includeintmain(){std::ifstreamfile("path/to/file");std::vectorvec(std::istream_iterator{file},{});//如果我理解正确,第二个参数被推断为std::istream_iterator类型其中调用了默认构造函数。适当std::vector构造函数声明为:templatevector(InputIteratorfirst,InputIteratorlast,constalloca

C++14 元编程 : Automagically build a list of types at compile/init time

使用C++14和CuriouslyRecurringTemplatePattern(CRTP)以及可能的Boost.Hana的某种组合(或boost::mpl如果您愿意),我可以在编译时(或静态初始化时)构建一个类型列表而无需显式声明吗?例如,我有这样的东西(在Coliru上查看):#include#include#includenamespace{structD1{staticconstexprautoval=10;};structD2{staticconstexprautoval=20;};structD3{staticconstexprautoval=30;};}intmain(