cppinsight对一些语言特性的解释
Posted 奇妙之二进制
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了cppinsight对一些语言特性的解释相关的知识,希望对你有一定的参考价值。
range based for loop循环
#include <cstdio>
int main()
const char arr[]2,4,6,8,10;
for(const char& c : arr)
printf("c=%c\\n", c);
#include <cstdio>
int main()
const char arr[5] = 2, 4, 6, 8, 10;
const char (&__range1)[5] = arr;
const char * __begin1 = __range1;
const char * __end1 = __range1 + 5L;
for(; __begin1 != __end1; ++__begin1)
const char & c = *__begin1;
printf("c=%c\\n", static_cast<int>(c));
return 0;
花括号初始化
int main()
// The compiler automatically fills the remaining
// fields with 0 thanks to braced initialization.
int arr[5]2,3,4;
int main()
int arr[5] = 2, 3, 4, 0, 0;
return 0;
struct A
// user provided constructor _missing_ initialzation of `j`
A()
: i3
int i;
int j;
;
struct B
// uses the default constructor
int i;
int j;
;
int main()
A a;
A a2; // only i gets initialized.
B b;
B b2; // both i and j get initialized.
struct A
inline A()
: i3
int i;
int j;
;
struct B
int i;
int j;
// inline B() noexcept = default;
;
int main()
A a = A();
A a2 = A;
B b = B();
B b2 = 0, 0;
return 0;
auto推导
int main()
auto i = 3; // int
auto a = 42; // initializer list
auto b42; // int
auto c = 1, 2; // initializer list
#include <initializer_list>
int main()
int i = 3;
std::initializer_list<int> a = std::initializer_list<int>42;
int b = 42;
std::initializer_list<int> c = std::initializer_list<int>1, 2;
return 0;
lamba表达式
int main()
int (*fp)(int, char) = [](int a, char b) return a+b;;
int main()
class __lambda_3_26
public:
inline /*constexpr */ int operator()(int a, char b) const
return a + static_cast<int>(b);
using retType_3_26 = int (*)(int, char);
inline constexpr operator retType_3_26 () const noexcept
return __invoke;
private:
static inline /*constexpr */ int __invoke(int a, char b)
return __lambda_3_26.operator()(a, b);
;
using FuncPtr_3 = int (*)(int, char);
FuncPtr_3 fp = static_cast<int (*)(int, char)>(__lambda_3_26.operator __lambda_3_26::retType_3_26());
return 0;
这个非常有意思,一个lamba表达式其实是一个类,它实现了函数调用运算符()重载,以及类型转换函数。
C++14的通用lamba
int main()
// Generic lambdas have a method template call operator.
auto x = [](auto x) return x * x; ;
x(2); // int
x(3.0); // double
int main()
class __lambda_4_12
public:
template<class type_parameter_0_0>
inline /*constexpr */ auto operator()(type_parameter_0_0 x) const
return x * x;
#ifdef INSIGHTS_USE_TEMPLATE
template<>
inline /*constexpr */ int operator()<int>(int x) const
return x * x;
#endif
#ifdef INSIGHTS_USE_TEMPLATE
template<>
inline /*constexpr */ double operator()<double>(double x) const
return x * x;
#endif
private:
template<class type_parameter_0_0>
static inline /*constexpr */ auto __invoke(type_parameter_0_0 x)
return __lambda_4_12.operator()<type_parameter_0_0>(x);
;
__lambda_4_12 x = __lambda_4_12;
x.operator()(2);
x.operator()(3.0);
return 0;
int x = 1;
auto Bar() -> decltype(x)
int x = 22;
// do some fancy calculation with x
return (x);
int x = 1;
int Bar()
int x = 22;
return (x);
lamba值捕获原理
class A
;
int main()
A a;
auto f = [a](int i) return i;;
class A
public:
// inline constexpr A() noexcept = default;
// inline constexpr A(const A &) noexcept = default;
;
int main()
A a = A();
class __lambda_7_11
public:
inline /*constexpr */ int operator()(int i) const
return i;
private:
A a;
public:
__lambda_7_11(const A & _a)
: a_a
;
__lambda_7_11 f = __lambda_7_11a;
return 0;
引用捕获原理
class A
;
int main()
A a;
auto f = [&a](int i) return i;;
class A
public:
// inline constexpr A() noexcept = default;
;
int main()
A a = A();
class __lambda_7_11
public:
inline /*constexpr */ int operator()(int i) const
return i;
private:
A & a;
public:
__lambda_7_11(A & _a)
: a_a
;
__lambda_7_11 f = __lambda_7_11a;
return 0;
this捕获原理
class A
void fun()
auto f = [this](int i) return i;;
;
int main()
A a;
class A
inline void fun()
class __lambda_3_12
public:
inline /*constexpr */ int operator()(int i) const
return i;
private:
A * __this;
public:
__lambda_3_12(A * _this)
: __this_this
;
__lambda_3_12 f = __lambda_3_12this;
public:
// inline constexpr A() noexcept = default;
;
int main()
A a = A();
return 0;
以上是关于cppinsight对一些语言特性的解释的主要内容,如果未能解决你的问题,请参考以下文章