小编nya*_*108的帖子

一个类不能有自己的静态constexpr成员实例吗?

这段代码给了我不完整的类型错误.问题是什么?一个类不允许拥有自己的静态成员实例吗?有没有办法达到同样的效果?

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)

c++ static-members c++11

41
推荐指数
3
解决办法
3194
查看次数

没有 bool 数组的类模板特化?

根据https://en.cppreference.com/std::vector<bool>具有类模板专业化,而std::array<bool, N>没有。不提供的原因有哪些?

c++ stdvector template-specialization class-template stdarray

27
推荐指数
4
解决办法
1205
查看次数

为什么必须在向前声明时提供枚举的大小?

我只是不明白为什么枚举的大小与编译器相关,而类的大小不是.

我的代码示例:

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让它编译并正常工作.所以问题是:

为什么这还没有标准化?

c++ enums forward-declaration c++11

20
推荐指数
2
解决办法
1057
查看次数

新建和删除操作符在库中覆盖

如果两个库(动态链接)有自己的全局覆盖版本的newdelete运算符并且他们使用自己的内存管理会发生什么?

在库中运送内存管理工具通常是错误的,还是在某些情况下仅为某些特定类提供内存管理是好的,只定义特定于类的操作符和删除操作符覆盖?

在静态链接库的情况下是否存在一些差异?

c++ new-operator delete-operator

15
推荐指数
2
解决办法
1255
查看次数

如何使用Google Mock中的可选参数模拟方法?

如何使用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)

c++ mocking googletest googlemock

15
推荐指数
3
解决办法
7425
查看次数

为什么switch和if语句与转换运算符的行为不同?

为什么switchif语句在转换运算符方面表现不同?

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 integerif声明中它被完美地识别为a bool.(GCC)

c++ if-statement switch-statement conversion-operator explicit-conversion

15
推荐指数
2
解决办法
2101
查看次数

使用C库头的C++程序将"this"识别为关键字.外部"C"错误?

我的C++程序需要使用外部C库.因此,我正在使用

extern "C"
{
   #include <library_header.h>
}
Run Code Online (Sandbox Code Playgroud)

我需要使用的每个模块的语法.

它一直运作到现在.模块在其头文件中使用名称作为某些变量.C库本身编译得很好,因为据我所知,从来就不是C中的关键字.

但是,尽管我使用了extern"C"语法,但当我包含该头文件时,我的C++程序出错了.

如果我每次重命名与类似的东西C库头文件_this,一切似乎都正常工作.

问题是:

不应在外部的"C"的语法够向后兼容性,至少在语法级别,对于一个头文件?这是编译器的问题吗?

c c++ this header-files extern

12
推荐指数
2
解决办法
1343
查看次数

模板类的模糊多重继承

我有一个真实的情况,可以在下面的例子中总结:

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

11
推荐指数
2
解决办法
732
查看次数

为什么std :: copy_if签名不限制谓词类型

想象一下,我们有以下情况:

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)

c++ stl stl-algorithm c++11

11
推荐指数
2
解决办法
769
查看次数

-O3(优化级别3)有什么问题?

我注意到在QT Creator中,发布版本的默认优化级别是-O2.我在想:为什么不-O3呢?我在这里读过Stack Overflow,它可能是危险的或"bug暴露",但那些被认为风险大于有用的优化标志是什么?为什么?

优化级别3标志(在GCC上):

  • -fgcse-after-reload
  • -finline-functions
  • -fipa-cp-clone
  • -fpredictive-commoning
  • -ftree-vectorize
  • -funswitch-loops

c c++ gcc compiler-optimization

10
推荐指数
1
解决办法
1888
查看次数