9得票2回答
一个构造函数中的 const int 引用能否安全地绑定到一个字面值?

我知道标准中有一种关于延长临时对象生命周期的例外,基本上是指在构造函数中绑定const引用不会延长生命周期,但这是否也适用于字面量?例如: class C { private: const int& ref; public: C(con...

9得票3回答
C++中的严格别名规则和类型别名

我正在尝试理解违反严格别名规则时的未定义行为。我已经阅读了许多关于此问题的文章,但仍有一个问题:我不太明白两种类型何时非法别名。cpp-reference指出: 类型别名 每当试图通过类型为AliasedType的glvalue读取或修改DynamicType类型对象的存储值时,除非以下情...

15得票1回答
静态模板化的constexpr嵌套类成员。

我有以下示例类Foo,其中嵌套了类Bar,而且所有内容都是constexpr: class Foo { private: template <typename T> struct Bar { constexpr Bar(){} ...

8得票3回答
为什么typedef模板是非法的?

从实际角度来看,我理解如果我们想让下面的代码编译通过,typedef和test都有点多余,需要被移除: template< typename type_t > typedef struct tagTest { int a; } test; 然而,我曾认为typedef...

27得票3回答
为什么选择这个类型转换运算符的重载?

Consider the following code. struct any { template <typename T> operator T &&() const; template <typename T> ...

13得票2回答
如果在lambda表达式中使用if constexpr和static_assert,哪个编译器是正确的?

当我们想在if constexpr中使用static_assert时,必须使条件依赖于一些模板参数。有趣的是,当代码被包装在lambda中时,gcc和clang存在争议。 以下代码可以在gcc下编译,但clang触发了assert,即使if constexpr为false。 #includ...

13得票1回答
为什么这个静态常量整数成员变量在数组定义中表现得像是可以公开访问的?

我做出以下声明:class Servo { protected: static const int maxServos = 16; static Servo servos[maxServos]; //Array declaration }; Servo Servo...

16得票2回答
当定义自定义点对象时,为什么删除函数是必要的?

自libstdc++的<concepts>头文件: namespace ranges { namespace __cust_swap { template<typename _Tp> void swap(_Tp&, _Tp&...

50得票4回答
为什么通过函数传递的临时对象,即使使用const引用也不能延长其生命周期?

在下面的简单示例中,为什么不能将 ref2 绑定到 min(x,y+1) 的结果? #include <cstdio> template< typename T > const T& min(const T& a, const T& b){ r...

7得票1回答
shared_ptr的析构函数是否需要使用“deleter”?

"这是 广泛地 已知的,只要指针可以在shared_ptr的构造期间被删除(并具有良好定义的行为),就可以使用shared_ptr来存储指向不完整类型的指针。例如,PIMPL技术:" struct interface { interface(); //...