Questions tagged «c++»

C ++是一种通用编程语言。它最初被设计为C的扩展,并且具有类似的语法,但是现在它是一种完全不同的语言。使用此标记可解决有关将要使用C ++编译器编译的代码的问题。对于与特定标准修订版[C ++ 11],[C ++ 14],[C ++ 17]或[C ++ 20]等相关的问题,请使用特定于版本的标记。

1
为什么约束函数允许未定义的行为?
C ++中的常量表达式具有非常整洁的特性:它们的求值不能具有未定义的行为(7.7.4.7): 表达式e是核心常量表达式,除非按照抽象机([intro.execution])的规则对e求值,将求出以下值之一: ... 一种操作,其操作将具有本文档的[引言] [cpp]中指定的未定义的行为[注意:例如,包括有符号整数溢出([expr.prop]),某些指针算术([expr.add],除以零或某些移位操作-尾注]; 尝试将13!in 的值存储在constexpr int确实产生一个不错的编译错误: constexpr int f(int n) { int r = n--; for (; n > 1; --n) r *= n; return r; } int main() { constexpr int x = f(13); return x; } 输出: 9:19: error: constexpr variable 'x' must be initialized …

3
为什么在运算符delete中不调用析构函数?
我试图在其中召唤::delete一堂课operator delete。但是不调用析构函数。 我定义的类MyClass,其operator delete过载。全局operator delete也超载。重载operator delete的MyClass将调用重载的全局operator delete。 class MyClass { public: MyClass() { printf("Constructing MyClass...\n"); } virtual ~MyClass() { printf("Destroying MyClass...\n"); } void* operator new(size_t size) { printf("Newing MyClass...\n"); void* p = ::new MyClass(); printf("End of newing MyClass...\n"); return p; } void operator delete(void* p) { printf("Deleting MyClass...\n"); ::delete p; …

9
Catalina C ++:使用<cmath>标头会产生错误:全局名称空间中没有名为“ signbit”的成员
从Mojave升级到Catalina后,在环境中设置:/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk。 我无法编译使用以下内容的程序 &lt;cmath&gt;标头。 我尝试更改CFLAGS,CCFLAGS,CXXFLAGS以指向没有任何改变的MacOSSDK位置 Scanning dependencies of target OgreMain /Applications/Xcode.app/Contents/Developer/usr/bin/make -f OgreMain/CMakeFiles/OgreMain.dir/build.make OgreMain/CMakeFiles/OgreMain.dir/build [ 0%] Building CXX object OgreMain/CMakeFiles/OgreMain.dir/src/OgreASTCCodec.cpp.o cd /Users/roman/Downloads/ogre-1.12.2/build/OgreMain &amp;&amp; /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ -DOgreMain_EXPORTS -D__ASSERT_MACROS_DEFINE_VERSIONS_WITHOUT_UNDERSCORES=0 -I/Users/roman/Downloads/ogre-1.12.2/OgreMain/src/OSX -I/Users/roman/Downloads/ogre-1.12.2/OgreMain/include/Threading -I/Users/roman/Downloads/ogre-1.12.2/OgreMain/src -I/Users/roman/Downloads/ogre-1.12.2/build/Dependencies/include -I/Users/roman/Downloads/ogre-1.12.2/OgreMain/include -I/Users/roman/Downloads/ogre-1.12.2/build/include -I/Users/roman/Downloads/ogre-1.12.2/OgreMain -isystem /usr/local/include -Wall -Winit-self -Wcast-qual -Wwrite-strings -Wextra -Wundef -Wmissing-declarations -Wno-unused-parameter -Wshadow -Wno-missing-field-initializers -Wno-long-long -Wno-inconsistent-missing-override -msse -O3 -DNDEBUG -arch …

4
确保在编译时在一个位置恰好调用了一个方法
我很好奇能否在编译时确保在一个地方恰好调用一种方法。 请注意,如果多次(例如在循环中)调用函数是可以的-但不应在两个单独的循环中调用该函数。 这可以分为两部分,我也对涵盖这两个部分的解决方案感兴趣: (a)确保在至少一个地方 调用方法(b)确保在一个地方最多调用方法 我对代码的结构拥有完全的控制权,欢迎实现相同想法的不同习惯用法。 // class.h class MyClass { public: void my_method(); } 以下内容不应编译(从不调用) #include "class.h" int main() { MyClass my_class; } 以下内容不应编译(在多个位置调用) #include "class.h" int main() { MyClass my_class; my_class.my_method(); while(true) { my_class.my_method(); } } 以下应编译(恰好在一个地方调用): #include "class.h" int main() { MyClass my_class; while(true) { my_class.my_method(); } …
15 c++ 

2
如何传递对模板typename参数的引用
有没有办法将引用作为参数传递给模板typename参数?我的意思是说,而不是通过int来传递对int的引用。 template &lt;typename T&gt; struct Foo { Foo(T arg) : ptr(arg) {} T ptr; }; int main() { int* a = new int(6); Foo&lt;decltype(a)&gt; foo1(a); // ptr is a copy of a pointer Foo&lt;decltype(&amp;a)&gt; foo1(&amp;a); // ptr seems to be a pointer to a pointer } 我知道我可以通过在类中使其成为T&来使'ptr'成员成为对指针的引用,但是我想知道是否可以通过传递给模板参数的参数来实现。
15 c++  templates 

5
如何在C ++中使用STL在低于总长度的位数下创建排列
我有一个c++ vector与std::pair&lt;unsigned long, unsigned long&gt;对象。我正在尝试使用生成矢量对象的排列std::next_permutation()。但是,我希望排列具有给定的大小,类似于permutationspython中指定了预期返回排列的大小的函数。 基本上,c++相当于 import itertools list = [1,2,3,4,5,6,7] for permutation in itertools.permutations(list, 3): print(permutation) Python演示 (1, 2, 3) (1, 2, 4) (1, 2, 5) (1, 2, 6) (1, 2, 7) (1, 3, 2) (1, 3, 4) .. (7, 5, 4) (7, 5, 6) (7, 6, 1) (7, …

3
确定最便宜参数类型的编译时方法
我有一个看起来像这样的模板 template &lt;typename T&gt; class Foo { public: Foo(const T&amp; t) : _t(t) {} private: const T _t; }; 有一种精明的模板元编程方法可以避免在参数类型像布尔值或char这样琐碎的情况下使用const引用吗?喜欢: Foo(stl::smarter_argument&lt;T&gt;::type t) : _t(t) {}
15 c++  stl 

4
允许从std :: map的键中窃取资源吗?
在C ++中,可以从以后不再需要的映射中窃取资源了吗?更准确地说,假设我有一个std::mapwith std::string键,并且我想通过使用窃取maps键的资源来构造一个向量std::move。请注意,对键的这种写访问会破坏的内部数据结构(键的顺序),map但此后我将不再使用它。 问题:我可以这样做没有任何问题吗?还是会导致意外的错误(例如,map由于我std::map以非预期的方式访问它)而导致了析构函数的错误? 这是一个示例程序: #include&lt;map&gt; #include&lt;string&gt; #include&lt;vector&gt; #include&lt;iostream&gt; using namespace std; int main(int argc, char *argv[]) { std::vector&lt;std::pair&lt;std::string,double&gt;&gt; v; { // new scope to make clear that m is not needed // after the resources were stolen std::map&lt;std::string,double&gt; m; m["aLongString"]=1.0; m["anotherLongString"]=2.0; // // now steal resources for (auto &amp;p …

1
C ++编译器如何找到extern变量?
我用g ++和clang ++编译该程序。有一个区别: g ++打印1,而clang ++打印2。 似乎 g ++:extern变量在最短范围内定义。 clang ++:extern变量是在最短的全局范围内定义的。 C ++规范对此有任何规范吗? main.cpp #include &lt;iostream&gt; static int i; static int *p = &amp;i; int main() { int i; { extern int i; i = 1; *p = 2; std::cout &lt;&lt; i &lt;&lt; std::endl; } } other.cpp int i; 版本:g …

5
使用宏计算源文件行?
使用C / C ++预处理程序,是否可以将源文件中的行计数为宏或某种编译时可用的值?例如,我可以在下面替换MAGIC1,MAGIC2并MAGIC3在使用时以某种方式获得值4 MAGIC3吗? MAGIC1 // can be placed wherever you like before the relevant // lines - either right before them, or in global scope etc. foo(); MAGIC2 bar(); MAGIC2 baz(); MAGIC2 quux(); MAGIC2 // ... possibly a bunch of code here; not guaranteed to be in same …

2
为什么`std :: basic_ios`具有公共构造函数?
std::basic_ios有一个公共的构造函数: explicit basic_ios (std::basic_streambuf&lt;CharT,Traits&gt;* sb); IMO,一个类具有公共构造函数的唯一原因是在程序中使用该类的独立实例。如果仅存在一个类以使其他类从其派生(如的情况basic_ios),则该类的所有构造函数都应为protected。的构造函数std::ios_base均受保护。但是,由于某种原因,该标准的设计者将这一构造函数basic_ios公开了。 basic_ios用作几种流类型的基类,并且我不能设想用例中至少有一个不是a basic_istream或的用例basic_ostream。有一个吗?

5
如何在for循环中使用const变量来生成模板类?
我有一个类似的代码 template &lt;size_t N&gt; class A { template &lt;size_t N&gt; someFunctions() {}; }; 现在,我想创建该类的实例,并在for循环中为一组许多值(例如, // in main() int main() { for (int i = 1; i &lt;= 100; i++) { const int N = i; // dont know how to do this A&lt;N&gt; a; a.functionCalls(); } } 这个怎么做?希望有一种方法可以做到这一点。

2
使用函数对象的C ++线程,如何调用多个析构函数,而不是构造函数?
请在下面找到代码片段: class tFunc{ int x; public: tFunc(){ cout&lt;&lt;"Constructed : "&lt;&lt;this&lt;&lt;endl; x = 1; } ~tFunc(){ cout&lt;&lt;"Destroyed : "&lt;&lt;this&lt;&lt;endl; } void operator()(){ x += 10; cout&lt;&lt;"Thread running at : "&lt;&lt;x&lt;&lt;endl; } int getX(){ return x; } }; int main() { tFunc t; thread t1(t); if(t1.joinable()) { cout&lt;&lt;"Thread is joining..."&lt;&lt;endl; t1.join(); } …

3
给定整数N。大于N且仅以0或1为数字的最小整数是什么?
我有一个整数N。我必须找到大于N的最小整数,该整数不包含0或1之外的任何数字。例如:如果N = 12答案为100。我已经用C ++编写了一种蛮力方法。 int main() { long long n; cin &gt;&gt; n; for (long long i = n + 1; ; i++) { long long temp = i; bool ok = true; while (temp != 0) { if ( (temp % 10) != 0 &amp;&amp; (temp % 10) != …
15 c++  algorithm 

1
旧的alloctaor :: construct与新的alloctaor和显式构造函数之间有什么区别?
据我所知std::allocator&lt;T&gt;::construct,在旧版本的C ++上仅需要两个参数;第一个是指向未构建的原始内存的指针,我们要在其中构造一个类型的对象,T第二个是用于初始化该对象的元素类型的值。因此,调用了复制构造函数: struct Foo { Foo(int, int) { cout &lt;&lt; "Foo(int, int)" &lt;&lt; endl; } /*explicit*/ Foo(int) { cout &lt;&lt; "Foo(int)" &lt;&lt; endl; } Foo(const Foo&amp;) { cout &lt;&lt; "Foo(const Foo&amp;)" &lt;&lt; endl; } }; int main(int argc, char* argv[]) { allocator&lt;Foo&gt; a; Foo* const p = a.allocate(200, NULL); // …
15 c++  allocator 

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.