以下代码在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
代码优先.
#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?
在Linux下,当进程崩溃时,将创建核心转储.
但是,我希望在进程没有崩溃时创建核心转储,但看起来有问题.远程专家需要核心转储进行分析.
在Windows下,我们可以通过任务管理器创建进程的转储文件,之后,该进程仍在运行.
在Linux下有可能吗?
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++标准访问全局变量?
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不是.
以下代码被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) 根据cppreference.com,std::rel_ops::operator!=,>,<=,>=将在C++ 20中弃用.
背后的理由是什么?
以下代码段摘自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&?
#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.输出是:
Run Code Online (Sandbox Code Playgroud)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> > >
这意味着:pos_1 = pos_2;没关系,虽然pos_2 = pos_1;不行.
为什么std::begin()总是返回const_iterator而不是iterator在这种情况下?
考虑:
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++ ×9
c++11 ×6
standards ×5
lambda ×2
templates ×2
c ×1
c++17 ×1
c++20 ×1
clang ×1
coding-style ×1
constructor ×1
coredump ×1
debugging ×1
decltype ×1
deprecated ×1
final ×1
inheritance ×1
iterator ×1
linux ×1
oop ×1
performance ×1
type-safety ×1
type-traits ×1
visual-c++ ×1