草庐IT

c++ - 将 C 与 C++ 链接是否可以避免在 C 中合法但在 C++ 中不合法的未定义行为?

如果您有一个使用C编译器编译的C文件,并且为C而定义了C而不是C++的行为,您能否将它与C++文件链接并且没有未定义的行为?在blah.c(编译为C的文件)中structx{intblah;charbuf[];};externchar*get_buf(structx*base);externstructx*make_struct(intblah,intsize);blah_if.hextern"C"{structx;char*get_buf(structx*base);structx*make_struct(intblah,intsize);}some_random.cpp(使用C++

c++ - 为什么 std::make_tuple(7 + N...) 在 C++11 中是合法的?

以下代码在C++11中是合法的。templatestd::tuplef(){returnstd::make_tuple(7+N...);}什么意思? 最佳答案 首先看模板参数:template.即使可以将可变数量的模板参数提供给f,它们都必须是int类型.现在当您使用f,parameterunpacking(7+N...)将遵循模式7+N并展开为7+t1,7+t2,7+t3,...,7+tn因此,您最终会得到一个元组,其中包含的每个模板参数都增加了7。详细信息可以在第14.5.3节可变参数模板[temp.variadic]中找到。3

c++ - 为什么 std::make_tuple(7 + N...) 在 C++11 中是合法的?

以下代码在C++11中是合法的。templatestd::tuplef(){returnstd::make_tuple(7+N...);}什么意思? 最佳答案 首先看模板参数:template.即使可以将可变数量的模板参数提供给f,它们都必须是int类型.现在当您使用f,parameterunpacking(7+N...)将遵循模式7+N并展开为7+t1,7+t2,7+t3,...,7+tn因此,您最终会得到一个元组,其中包含的每个模板参数都增加了7。详细信息可以在第14.5.3节可变参数模板[temp.variadic]中找到。3

c++ - C++14 中 main() 的合法定义

我能找到的C++14的最后一个草稿说,关于main()[3.6.1]:Animplementationshallnotpredefinethemainfunction.Thisfunctionshallnotbeoverloaded.Itshallhaveareturntypeoftypeint,butotherwiseitstypeisimplementation-defined.Allimplementationsshallallowboth—afunctionof()returningintand—afunctionof(int,pointertopointertochar)re

c++ - C++14 中 main() 的合法定义

我能找到的C++14的最后一个草稿说,关于main()[3.6.1]:Animplementationshallnotpredefinethemainfunction.Thisfunctionshallnotbeoverloaded.Itshallhaveareturntypeoftypeint,butotherwiseitstypeisimplementation-defined.Allimplementationsshallallowboth—afunctionof()returningintand—afunctionof(int,pointertopointertochar)re

c++ - 在基于范围的 for 循环中将元素添加到该 vector 上的预分配 vector 是否合法?

我正在使用VisualStudio2015Update1C++编译器和此代码片段:#include#includeusingnamespacestd;intmain(){vectorv{3,1,4};v.reserve(6);for(autoe:v)v.push_back(e*e);for(autoe:v)cout发布版本运行良好,但调试版本产生vectoriteratorsincompatible错误消息。这是为什么呢?在将其标记为与Addelementstoavectorduringrange-basedloopc++11重复的问题之前,请阅读我的回答https://stackov

c++ - 在基于范围的 for 循环中将元素添加到该 vector 上的预分配 vector 是否合法?

我正在使用VisualStudio2015Update1C++编译器和此代码片段:#include#includeusingnamespacestd;intmain(){vectorv{3,1,4};v.reserve(6);for(autoe:v)v.push_back(e*e);for(autoe:v)cout发布版本运行良好,但调试版本产生vectoriteratorsincompatible错误消息。这是为什么呢?在将其标记为与Addelementstoavectorduringrange-basedloopc++11重复的问题之前,请阅读我的回答https://stackov

c++ - 修改 std::string::op[] 的结果是否合法?

考虑C++11中的以下内容:[C++11:21.4.5]:basic_stringelementaccess             [string.access]const_referenceoperator[](size_typepos)const;referenceoperator[](size_typepos);1  Requires:pos.2  Returns:*(begin()+pos)ifpos,otherwiseareferencetoanobjectoftypeTwithvaluecharT();thereferencedvalueshallnotbemodifie

c++ - 修改 std::string::op[] 的结果是否合法?

考虑C++11中的以下内容:[C++11:21.4.5]:basic_stringelementaccess             [string.access]const_referenceoperator[](size_typepos)const;referenceoperator[](size_typepos);1  Requires:pos.2  Returns:*(begin()+pos)ifpos,otherwiseareferencetoanobjectoftypeTwithvaluecharT();thereferencedvalueshallnotbemodifie

c++ - 为什么编译器支持冗余范围限定,它是否合法?

我在两个编译器上进行了测试,惊讶地发现它们都毫无怨言地支持以下定义:classA{A();};A::A::A(){}^^^请注意,这也适用于方法,尽管它在声明被过度限定时被标记。问题:这是一个有效的C++程序吗?如果是这样,它的用途是什么-还是仅仅是副产品?更新详情:如果最初的问题不清楚或太短:我很好奇为什么定义上允许有多余的限定词(上面还添加了强调)。Clang一个Apple的GCC4.2+LLVM是编译器 最佳答案 是的,这是允许的(§9/2):Theclass-nameisalsoinsertedintothescopeoft