这段代码给了我不完整的类型错误.问题是什么?一个类不允许拥有自己的静态成员实例吗?有没有办法达到同样的效果?
struct Size
{
const unsigned int width;
const unsigned int height;
static constexpr Size big = { 480, 240 };
static constexpr Size small = { 210, 170 };
private:
Size( ) = default;
};
Run Code Online (Sandbox Code Playgroud) 根据https://en.cppreference.com/,std::vector<bool>
具有类模板专业化,而std::array<bool, N>
没有。不提供的原因有哪些?
c++ stdvector template-specialization class-template stdarray
我只是不明白为什么枚举的大小与编译器相关,而类的大小不是.
我的代码示例:
class A;
enum E; // must be enum E : int; in order to compile
void f(const A & param);
void f(const E & param);
Run Code Online (Sandbox Code Playgroud)
我在这里谈论标准C++编译器.我知道MSVC让它编译并正常工作.所以问题是:
为什么这还没有标准化?
如果两个库(动态链接)有自己的全局覆盖版本的new和delete运算符并且他们使用自己的内存管理会发生什么?
在库中运送内存管理工具通常是错误的,还是在某些情况下仅为某些特定类提供内存管理是好的,只定义特定于类的新操作符和删除操作符覆盖?
在静态链接库的情况下是否存在一些差异?
如何使用Google Mock中的可选参数模拟方法?例如:
class A
{
public:
void set_enable( bool enabled = true );
};
class MockA : public A
{
MOCK_METHOD1( set_enable, void( bool ) ); // this is not working
};
Run Code Online (Sandbox Code Playgroud) 为什么switch
和if
语句在转换运算符方面表现不同?
struct WrapperA
{
explicit operator bool() { return false; }
};
struct WrapperB
{
explicit operator int() { return 0; }
};
int main()
{
WrapperA wrapper_a;
if (wrapper_a) { /** this line compiles **/ }
WrapperB wrapper_b;
switch (wrapper_b) { /** this line does NOT compile **/ }
}
Run Code Online (Sandbox Code Playgroud)
编译错误是switch quantity is not an integer
在if
声明中它被完美地识别为a bool
.(GCC)
c++ if-statement switch-statement conversion-operator explicit-conversion
我的C++程序需要使用外部C库.因此,我正在使用
extern "C"
{
#include <library_header.h>
}
Run Code Online (Sandbox Code Playgroud)
我需要使用的每个模块的语法.
它一直运作到现在.模块在其头文件中使用此名称作为某些变量.C库本身编译得很好,因为据我所知,这从来就不是C中的关键字.
但是,尽管我使用了extern"C"语法,但当我包含该头文件时,我的C++程序出错了.
如果我每次重命名这与类似的东西C库头文件_this,一切似乎都正常工作.
问题是:
不应在外部的"C"的语法够向后兼容性,至少在语法级别,对于一个头文件?这是编译器的问题吗?
我有一个真实的情况,可以在下面的例子中总结:
template< typename ListenerType >
struct Notifier
{
void add_listener( ListenerType& ){}
};
struct TimeListener{ };
struct SpaceListener{ };
struct A : public Notifier< TimeListener >
, public Notifier< SpaceListener >
{
};
struct B : TimeListener{ };
int main()
{
A a;
B b;
a.add_listener( b ); // why is ambiguous?
return 0;
}
Run Code Online (Sandbox Code Playgroud)
为什么编译器不明显B
是a TimeListener
,因此唯一可能的重载解决方案是Notifier< TimeListener >::add_listener( TimeListener& )
?
c++ multiple-inheritance overload-resolution name-lookup template-classes
想象一下,我们有以下情况:
struct A
{
int i;
};
struct B
{
A a;
int other_things;
};
bool predicate( const A& a)
{
return a.i > 123;
}
bool predicate( const B& b)
{
return predicate(b.a);
}
int main()
{
std::vector< A > a_source;
std::vector< B > b_source;
std::vector< A > a_target;
std::vector< B > b_target;
std::copy_if(a_source.begin(), a_source.end(), std::back_inserter( a_target ), predicate);
std::copy_if(b_source.begin(), b_source.end(), std::back_inserter( b_target ), predicate);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这两个调用都会std::copy_if
产生编译错误,因为predicate()
编译器不能接受函数的正确重载,因为std::copy_if
模板签名接受任何类型的谓词:
template<typename _IIter,
typename _OIter, …
Run Code Online (Sandbox Code Playgroud) 我注意到在QT Creator中,发布版本的默认优化级别是-O2
.我在想:为什么不-O3
呢?我在这里读过Stack Overflow,它可能是危险的或"bug暴露",但那些被认为风险大于有用的优化标志是什么?为什么?
优化级别3标志(在GCC上):
-fgcse-after-reload
-finline-functions
-fipa-cp-clone
-fpredictive-commoning
-ftree-vectorize
-funswitch-loops
c++ ×10
c++11 ×3
c ×2
enums ×1
extern ×1
gcc ×1
googlemock ×1
googletest ×1
header-files ×1
if-statement ×1
mocking ×1
name-lookup ×1
new-operator ×1
stdarray ×1
stdvector ×1
stl ×1
this ×1