小编Seo*_*oul的帖子

if语句中的复合表达式

我偶然发现了这样做的能力.

#include <iostream>
using namespace std;

int main() {
    if ( ({int i = 1024; i == 10;}) ) {
        cout << "In" << endl;
    }
}
Run Code Online (Sandbox Code Playgroud)

重要的拆卸区域似乎是:

->  0x10000118f <+15>: movl   $0x400, -0x18(%rbp)       ; imm = 0x400 
    0x100001196 <+22>: cmpl   $0xa, -0x18(%rbp)
    0x10000119a <+26>: sete   %al
    0x10000119d <+29>: andb   $0x1, %al
    0x10000119f <+31>: movb   %al, -0x19(%rbp)
    0x1000011a2 <+34>: testb  $0x1, -0x19(%rbp)
    0x1000011a6 <+38>: je     0x1000011d9               ; <+89> at main.cpp:37
Run Code Online (Sandbox Code Playgroud)

从检查这一点来看,它似乎需要将最后一个语句(比较i == 10)作为if语句的布尔值.

我理解这种情况不允许我i在if语句中使用变量,因为scope运算符,但想知道 …

c++ gcc if-statement

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

具有默认值的模板专业化

这是我正在使用的代码:

#include <iostream>
#include <type_traits>

using namespace std;

using match_type = void;
using match_type_bad = int;

// version A
template <typename T, typename Attempt = match_type>
struct simple_check : false_type {}; 

// version B
template <typename T>
struct simple_check<T, T> : true_type {}; 

int main() {
  cout << simple_check<match_type>::value << endl;
  cout << simple_check<match_type_bad>::value << endl;
}
Run Code Online (Sandbox Code Playgroud)

具有此模板专长的程序最终输出为:

1
0
Run Code Online (Sandbox Code Playgroud)

我对C ++的tmp的理解有些混乱,因为我一直认为输出应该是1 1

我的理由是:

  1. 随着simple_check<match_type>它进入版本B,然后扩展到simple_check<match_type, match_type>从继承true_type。如此1预期。

  2. 难道不一样simple_check<match_type_bad>吗?

  3. 有了这个逻辑,任何类型的 …

c++ template-meta-programming c++11 c++14 c++17

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

为什么[范围(10)]和列表(范围(10))不一样?

为什么[range(10)]list(range(10))在Python 3有什么不同?

输出如下:

>>> print([range(10)])
[range(0, 10)]
>>> print(list(range(10)))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Run Code Online (Sandbox Code Playgroud)

python list range python-3.x

3
推荐指数
2
解决办法
2705
查看次数

C++ 11多个移动构造函数调用

我编写了这个,g++ -std=c++11 file.cpp我很困惑通过研究移动构造函数C++.

#include <iostream>
#include <string>
#include <vector>
using namespace std;

class P {
public:
    string* ptr_;
    P(string name) { ptr_ = new string(name); }
    ~P() { delete ptr_; }
    P(P&& pother) : ptr_(move(pother.ptr_)) { 
        cout<<"move"<<endl; 
        pother.ptr_=nullptr; 
    }
    void print() {cout << *ptr_ << endl;}
};

int main()
{
    vector<P> ppl;
    ppl.push_back(P("Jojo"));
    ppl.push_back(P("Jojo"));
    ppl.push_back(P("Jojo"));
}
Run Code Online (Sandbox Code Playgroud)

该程序的输出是:

$ ./a.out 
move
move
move
move
move
move
Run Code Online (Sandbox Code Playgroud)

为什么移动构造函数在这里调用了6次?

c++ vector stdvector c++11

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

C++ async + future(延迟vs异步)

这是我正在使用的测试程序.有人可以详细描述正在发生的事情以及输出的原因.

为什么launch::async获得g_num价值0,而launch::deferred获得100.

双方launch::asynclaunch::differed得到了正确的价值观arg是在上main堆,我相信意味着他们应该有两个得到100.

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

thread_local int g_num;

int read_it(int x) {
    return g_num + x;
}
int main()
{
    g_num = 100;
    int arg;
    arg = 1;
    future<int> fut = async(launch::deferred, read_it, arg);
    arg = 2;
    future<int> fut2 = async(launch::async, read_it, arg);
    cout << "Defer: " << fut.get() << endl;
    cout << "Async: " …
Run Code Online (Sandbox Code Playgroud)

c++ multithreading asynchronous c++11 c++14

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

为什么在SFINAE期间需要参数中的指针?

为什么我需要在线*上制作checker指针

template <typename C> static yes test( checker<C, &C::helloworld>* );

为了使编译时间扣除正常工作,输出1 0

当我删除时*,输出是0 0

#include <iostream>

struct Generic {}; 
struct Hello
{ int helloworld() { return 0; } };

// SFINAE test
template <typename T>
class has_helloworld
{
    typedef char                yes;
    typedef struct {char _[2];}  no; 

    template <typename C, int (C::*)()> struct checker;

    template <typename C> static yes test( checker<C, &C::helloworld>* );
    template <typename C> static no  test(...);

public:
    enum { …
Run Code Online (Sandbox Code Playgroud)

c++ templates sfinae c++03 c++98

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

使用auto作为成员函数参数时的结果不同

运行这两个时,我得到了不同的结果

我在 GNU/Linux 4.14.67

这两者都使用g++ -std=c++14with/without -O0和on 运行c++17.

为什么我?为什么输出不同?

第一个版本是:

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

class foo {
public:
    foo() { }
    foo(const foo& f) { }
    foo& operator=(const foo& f) {
        cout << "foo operator=\n";
        val = 888;
        // Do something important
        return *this;
    }
    int val;
};

int main() {
    foo f1;
    foo f2;
    f1 = f2;

    cout << f1.val << endl;
}
Run Code Online (Sandbox Code Playgroud)

第一个输出是:

foo operator=
888
Run Code Online (Sandbox Code Playgroud)

第二个版本(仅const foo&改为 …

c++ c++14 c++17

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

C全局静态变量初始化是由链接器完成的吗?

假设我们有:

在f1.c

#include <stdio.h>
static int x = 10;

void f1() {
  printf("f1.c : %d\n", x);
}
Run Code Online (Sandbox Code Playgroud)

main.c中

extern void f1();
int main(int argc, char **argv) {
  f1();
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

我们将编译和读取两个ELF文件符号表(rel.ELF和exec ELF):

$> gcc -c *.c
$> readelf -s f1.o | grep x
      Num:    Value          Size Type    Bind   Vis      Ndx Name
        5: 0000000000000000     4 OBJECT  LOCAL  DEFAULT    3 x
$> gcc *.o
$> readelf -s a.out | grep x
      Num:    Value          Size Type    Bind   Vis      Ndx Name
       38: 0000000000601038 …
Run Code Online (Sandbox Code Playgroud)

c linker gcc symbol-table

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