我偶然发现了这样做的能力.
#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运算符,但想知道 …
这是我正在使用的代码:
#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。
我的理由是:
随着simple_check<match_type>它进入版本B,然后扩展到simple_check<match_type, match_type>从继承true_type。如此1预期。
难道不一样simple_check<match_type_bad>吗?
有了这个逻辑,任何类型的 …
为什么[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) 我编写了这个,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次?
这是我正在使用的测试程序.有人可以详细描述正在发生的事情以及输出的原因.
为什么launch::async获得g_num价值0,而launch::deferred获得100.
双方launch::async并launch::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) 为什么我需要在线*上制作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) 运行这两个时,我得到了不同的结果
我在 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&改为 …
假设我们有:
在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++ ×6
c++11 ×3
c++14 ×3
c++17 ×2
gcc ×2
asynchronous ×1
c ×1
c++03 ×1
c++98 ×1
if-statement ×1
linker ×1
list ×1
python ×1
python-3.x ×1
range ×1
sfinae ×1
stdvector ×1
symbol-table ×1
templates ×1
vector ×1