小编xml*_*lmx的帖子

为什么std :: make_tuple(7 + N ...)在C++ 11中合法?

以下代码在C++ 11中是合法的.

template<int... N>
std::tuple<decltype(N)...> f()
{
    return std::make_tuple(7 + N...); 
}
Run Code Online (Sandbox Code Playgroud)

这是什么意思?

c++ templates compile-time-constant variadic-templates c++11

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

如何检测C++ 11中的类是否是最终的?

代码优先.

#include <iostream>

using namespace std;

struct A final {};
struct B {};

int main()
{ 
    cout << is_final<A>::value << endl; // Output true
    cout << is_final<B>::value << endl; // Output false

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

如何实现类is_final?

c++ final type-traits c++11

19
推荐指数
2
解决办法
1476
查看次数

即使进程正常运行,如何创建核心转储?

在Linux下,当进程崩溃时,将创建核心转储.

但是,我希望在进程没有崩溃时创建核心转储,但看起来有问题.远程专家需要核心转储进行分析.

在Windows下,我们可以通过任务管理器创建进程的转储文件,之后,该进程仍在运行.

在Linux下有可能吗?

c linux debugging coredump remote-debugging

19
推荐指数
4
解决办法
2387
查看次数

lambda捕获什么都不能访问全局变量?

int n;    
int main()
{
    [](){ n = 0; }(); // clang says "ok"

    int m;
    [](){ m = 0; }(); // clang says "not ok"
}
Run Code Online (Sandbox Code Playgroud)

我只是好奇:

如果lambda没有捕获任何内容,是否允许按照C++标准访问全局变量?

c++ lambda standards global-variables c++11

19
推荐指数
3
解决办法
3879
查看次数

总是在lambda表达式中捕获所有内容是不好的做法?

std::function<int()> void f1()
{
    int a, b, c, d, ..., x, y, z;

    return [=] { return a + b + c; };
}
Run Code Online (Sandbox Code Playgroud)

std::function<int()> void f2()
{
    int a, b, c, d, ..., x, y, z;

    return [a, b, c] { return a + b + c; };
}
Run Code Online (Sandbox Code Playgroud)

毋庸置疑,前者比后者更短,更方便,更优雅.

但是,我仍然担心:

从性能的角度来看,后者总是优于前者吗?

标准是否保证lambda表达式仅捕获必要的变量?即,在前一示例中,仅捕获a,b,c,未使用的变量d,...,x,y,z不是.

c++ performance lambda coding-style c++11

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

如果基类是成员函数的参数类型,是否需要指定基类的模板参数?

以下代码被VC++ 2013接受,但被clang 3.4拒绝.

哪个编译器符合C++标准?

template<class T>
struct A
{
    T n;
};

template<class T>
struct B : A<T>
{
    // VC++ 2013 : OK
    // clang : error : use of class template 'A' requires template arguments
    B& f1(const A& obj) 
    {
        return *this;
    }

    // VC++ : OK
    // clang : OK
    B& f2(const A<T>& obj)
    {
        return *this;
    }
};

int main()
{
    B<int> b;
}
Run Code Online (Sandbox Code Playgroud)

c++ standards templates clang visual-c++

18
推荐指数
2
解决办法
415
查看次数

18
推荐指数
2
解决办法
727
查看次数

为什么"const auto [x,y]"在绑定到引用类型时不能按预期运行?

以下代码段摘自cppref:

std::tuple<int, int&> f();

auto [x, y] = f(); 
// decltype(x) is int
// decltype(y) is int&

const auto [z, w] = f();
// decltype(z) is const int
// decltype(w) is int&
Run Code Online (Sandbox Code Playgroud)

我的问题是在最后一行:

为什么 decltype(w) int& 不是 const int&

c++ standards decltype c++17 structured-bindings

18
推荐指数
2
解决办法
408
查看次数

为什么"std :: begin()"在这种情况下总是返回"const_iterator"?

#include <vector>
#include <iostream>

using namespace std;

int main()
{
    vector<int> coll;

    decltype(std::begin(std::declval<vector<int>>()))
        pos_1 = coll.begin();
    auto pos_2 = coll.begin();

    cout << typeid(decltype(pos_1)).name() << endl;
    cout << typeid(decltype(pos_2)).name() << endl;
}
Run Code Online (Sandbox Code Playgroud)

我的编译器是clang 4.0.输出是:

class std::_Vector_const_iterator<class std::_Vector_val<struct std::_Simple_types<int> > >
class std::_Vector_iterator<class std::_Vector_val<struct std::_Simple_types<int> > >
Run Code Online (Sandbox Code Playgroud)

这意味着:pos_1 = pos_2;没关系,虽然pos_2 = pos_1;不行.

为什么std::begin()总是返回const_iterator而不是iterator在这种情况下?

c++ standards iterator type-safety c++11

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

为什么不能公开继承的受保护构造函数?

考虑:

class A
{
protected:
    A(int) {}
    void f(int) {}

public:
    A() {}
};

class B : public A
{
public:
    using A::A;
    using A::f;
};

int main()
{
    B().f(1); // ok
    B(1); // error: 'A::A(int)' is protected within this context
}
Run Code Online (Sandbox Code Playgroud)

为什么不能创建继承的protected构造函数public,而继承的protected成员函数可以?

c++ oop inheritance constructor c++11

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