是否std::is_move_constructible<T>::value == true暗示T有一个可用的移动构造函数?如果是这样,它的默认行为是什么?
考虑以下情况:
struct foo {
int* ptr;
};
int main() {
{
std::cout << std::is_move_constructible<foo>::value << '\n';
foo f;
f.ptr = (int*)12;
foo f2(std::move(f));
std::cout << f.ptr << ' ' << f2.ptr << '\n';
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出是:
1
0000000C 0000000C
Run Code Online (Sandbox Code Playgroud)
我认为那f.ptr应该是nullptr.所以在这种情况下,
f2移动构建?(我正在使用VS11.)
移动构造函数的默认行为与复制构造函数相同,是否正确?如果这是真的,
foo f2(std::move(f));当我宣布一个时,似乎调用了copy ctor,参见:
struct foo {
int* ptr;
foo() {}
foo(const foo& other) {
std::cout << …Run Code Online (Sandbox Code Playgroud) 我一直认为WriteFile比fwrite更有效,因为fwrite在内部调用WriteFile,但是下面的测试代码告诉我fwrite比WriteFile更快.
写入成本为2毫秒,而WriteFile需要27000(FILE_ATTRIBUTE_NORMAL),每次写入调用后都会刷新.如果我用FILE_FLAG_WRITE_THROUGH调用WriteFile,并注释FlushFileBuffers(wfile)行,WriteFile会更快,成本为800.
那真的是fwrite调用WriteFile吗?是什么让这么大的差异?fwrite如何在内部工作?如何使用API比fwrite更有效地将数据写入文件?(unbufferd,synchronous).
#include <Windows.h>
#include <stdio.h>
#include <iostream>
int main() {
FILE* cfile = fopen("file1.txt", "w");
HANDLE wfile = CreateFile("file2.txt", GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS,
/*FILE_ATTRIBUTE_NORMAL*/FILE_FLAG_WRITE_THROUGH, NULL);
DWORD written = 0;
DWORD start_time, end_time;
char * text = "test message ha ha ha ha";
int size = strlen(text);
int times = 999;
start_time = timeGetTime();
for(int i = 0; i < times; ++i) {
fwrite(text, 1, size, cfile);
fflush(cfile);
}
end_time = timeGetTime();
std::cout << end_time - start_time << '\n'; …Run Code Online (Sandbox Code Playgroud) 示例代码取自:http://en.cppreference.com/w/cpp/types/add_cv (我修改了一下.)
struct foo
{
void m() { std::cout << "Non-cv\n"; }
void m() const { std::cout << "Const\n"; }
};
template<class T>
void call_m()
{
T().m();
}
int main()
{
call_m<foo>();
call_m<const foo>(); //here
}
Run Code Online (Sandbox Code Playgroud)
输出是:
Non-cv
Non-cv
Run Code Online (Sandbox Code Playgroud)
在第二次调用中,T是const限定的,所以T()应该调用const版本,对吧?还是有一些我错过的特殊规则?
我知道在c ++ 03中,非const引用不能绑定到rvalues.
T& t = getT();是无效的,在c ++ 11中,我们可以这样做:T&& t = getT(); 但是上面的代码怎么样,应该在c ++ 11中工作?
我用vs11测试了下面的代码:
Foo getFoo() {
return Foo();
}
void fz(Foo& f) {
}
int getInt() {
return int();
}
void iz(int& i) {
}
int main() {
{
Foo& z = getFoo(); //ok
fz(getFoo()); //ok
int& z2 = getInt(); //error: initial value of reference to non-const must be an lvalue
iz(getInt()); //same as above
}
}
Run Code Online (Sandbox Code Playgroud)
Foo是一个自定义类,我不明白为什么前两行编译.临时引用的z是在main的内部范围的末尾被破坏.标准是否对此有所说明?
class Foo {
public: …Run Code Online (Sandbox Code Playgroud) 我有一个在main的同一个文件中定义的类,另一个类(完整的静态函数/成员)在2个单独的文件中定义,它崩溃了.我想这可能与全局/静态实例的生命周期有关.似乎在ctor中,静态成员尚未初始化,并且可能发生在退出时,静态成员在第一个实例被销毁之前被释放.这是测试代码:
//testh.h
#include <map>
class Sc {
public:
static void insert();
static void out();
private:
static std::map<int, int> map_;
};
//testcpp.cpp
#include "testh.h"
#include <iostream>
std::map<int, int> Sc::map_;
void Sc::insert() {
map_.insert(std::make_pair(2,3));
}
void Sc::out() {
for(auto m : map_) {
std::cout << m.first << ' ' << m.second << '\n';
}
}
//main.cpp
#include "testh.h"
class Nc {
public:
Nc() {
Sc::insert();
Sc::out();
}
~Nc() {
Sc::insert();
Sc::out();
}
};
Nc nc;
int main() {
system("pause");
return 0;
} …Run Code Online (Sandbox Code Playgroud) 我对std::remove_extent(visual studio 11)的实现有疑问
template<class _Ty>
struct remove_extent
{
typedef _Ty type;
};
template<class _Ty, unsigned int _Ix>
struct remove_extent<_Ty[_Ix]>
{
typedef _Ty type;
};
template<class _Ty>
struct remove_extent<_Ty[]> //what for?
{
typedef _Ty type;
};
Run Code Online (Sandbox Code Playgroud)
我刚试过这个: std::cout << typeid(int[]).name() << '\n';
输出是:int [0],所以我认为_Ty[]代表_Ty[0].
但是专业的目的是什么_T[0],我认为第二个案件就是为此而处理的.
另外,我真的怀疑if是否T [0]是有效类型,如果是这样,在哪种情况下你会使用它?
c++ ×6
c++11 ×3
const-method ×1
file-io ×1
fwrite ×1
lvalue ×1
performance ×1
rvalue ×1
type-traits ×1
visual-c++ ×1
winapi ×1