小编Eis*_* N.的帖子

静态constexpr int vs老式枚举:何时以及为何?

这可能是一个基本问题,但我现在无法看到自己的反应.

请考虑以下代码:

template<bool b>
struct T {
    static constexpr int value = (b ? 42 : 0);
};

template<bool b>
struct U {
    enum { value = (b ? 42 : 0) };
};

int main() {
    static_assert(T<true>::value == 42, "!");
    static_assert(T<false>::value == 0, "!");
    static_assert(U<true>::value == 42, "!");
    static_assert(U<false>::value == 0, "!");
}
Run Code Online (Sandbox Code Playgroud)

我习惯使用类似的结构T,但我不止一次看过U用于相同目的的结构(主要是特征定义).

据我所知,它们都是在编译时解决的,它们解决了几乎相同的问题,但在我看来,它T比可读性更强U(我知道,我个人意见).

我的问题非常简单:有哪种技术原因可以解决哪一种解决方案比另一种更好?
更有甚者,其中一个不是一个可行的解决方案吗?

c++ enums constexpr c++11

23
推荐指数
3
解决办法
2989
查看次数

将具有默认参数的lambda函数复制到变量

请考虑以下代码:

#include <iostream>
#include <functional>
using namespace std;

int main() {
    auto f = [](int a = 3) {cout << a << endl; };
    f(2); // OK
    f();  // OK

    auto g = f;
    g(2); // OK
    g();  // OK

    function<void(int)> h = f;
    h(2); // OK
    h();  // Error! How to make this work?

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我怎么能声明和?h一样?fg

c++ lambda c++11

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

使用默认参数值作为模板参数传递函数

我对以下行为感到有点困惑.我传递一个带有两个参数的函数,一个具有默认值,作为模板参数,并使用一个参数调用该函数.为什么编译失败?而且,解决方案/解决方法是什么?

#include <iostream>
using namespace std;

template<typename Function> 
void eval(Function function) {
    function(10);
}

void sum(int i, int j = 0) {
    cout << "Sum is " << i + j;
}

int main() {
    sum(10);    // OK, of course :)
    eval(sum);  // Error!
}
Run Code Online (Sandbox Code Playgroud)

请注意,此问题与使用默认参数调用模板化函数无关.

错误消息:

prog.cpp: In instantiation of 'void eval(Function) [with Function = void (*)(int, int)]':
prog.cpp:15:10:   required from here
prog.cpp:6:10: error: too few arguments to function
  function(10);
          ^
Run Code Online (Sandbox Code Playgroud)

c++ templates

7
推荐指数
1
解决办法
954
查看次数

损坏的虚拟表视觉工作室bug

我遇到了一个由于构建和运行Visual Studio 2015中下面发布的示例代码而导致的损坏的虚拟表.

当我运行它时,在分配m_string时抛出异常.

我已经验证了代码使用gcc和Clang编译器构建并按预期运行.

我试图尽量减少这个例子,因为它是从一个非常大的项目中合成的.

此外,我意识到我从一些函数返回null - 实际返回值与问题无关,但返回类型可能是.这可能是Visual Studio的错误吗?

#include <iostream>
#include <memory>
#include <string>

struct A { virtual ~A(void) { } };
struct B { virtual ~B(void) { } };
struct C : public A, public B { virtual ~C(void) override { } };

struct D
{
    virtual ~D(void) { }
    virtual B *func(void) = 0;
};

struct E : public D
{
    virtual ~E(void) override { }
    virtual C *func(void) override { return nullptr; }
};

struct …
Run Code Online (Sandbox Code Playgroud)

c++ visual-studio-2010 visual-studio visual-studio-2012 visual-studio-2015

6
推荐指数
1
解决办法
356
查看次数

C++ std :: string初始化的行为不正确

为什么以下代码编译?它用clang和print编译并运行良好first.但是,我认为正确的行为应该是抱怨并发出正确的错误.

#include <iostream>
#include <string>

int main()
{
    std::string s{ "first", "second" };
    std::cout << s << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

这个问题的灵感来自于此.

c++ string stl

6
推荐指数
1
解决办法
146
查看次数

初始化struct成员时可能出现的MSVC 2013错误

MSVC 2013抱怨以下代码,而它在g ++中按预期工作.这看起来像MSVC中的错误吗?

#include <iostream>
using namespace std;

struct A
{
    double x = 0.0, y = 0.0;
};

int main()
{
    A a{ 1.0, 2.0 };
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

请注意,更改struct以下内容可解决此问题.

struct A
{
    double x, y;
};
Run Code Online (Sandbox Code Playgroud)

错误消息是:

错误1错误C2440:'初始化':无法从'initializer-list'转换为'A'

c++ initializer-list visual-c++ c++11

5
推荐指数
1
解决办法
640
查看次数

使用`reinterpret_cast`调用非静态成员函数而不进行实例化

如下面的代码所示,我可以在A::f不实例化类的对象的情况下调用非静态成员函数.仅当函数未绑定到任何其他成员时才可以执行此操作.例如,我不能A::g以类似的方式打电话.

在我看来,A::f下面代码中显示的调用就像调用静态成员函数一样.这样的结论是否正确?如何证明这种行为是正当的?

#include <iostream>
using namespace std;

struct A {
    void f() { cout << "Hello World!"; }    
    void g() { cout << i; } 
    int i = 10;
};

int main() {
    auto fPtr = reinterpret_cast<void(*)()>(&A::f);
    (*fPtr)(); // OK

//  auto gPtr = reinterpret_cast<void(*)()>(&A::g); 
//  (*gPtr)(); // Error!
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ casting

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