以下代码摘自cppreference.com.
#include <iostream>
#include <type_traits>
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<std::add_const<foo>::type>();
}
Run Code Online (Sandbox Code Playgroud)
但是,使用VC++ 2012年11月的CTP编译时,输出为
非CV
非CV
而不是预期的:
非CV
常量
此外,以下两个陈述之间的区别是什么:
call_m<const foo>();
和
call_m<std::add_const<foo>::type>();
我知道,琐碎小事std::string_view不能保证会以null结尾。但是,我不知道std::string_view文字是否保证为空终止。
例如:
#include <string_view>
using namespace std::literals;
int main()
{
auto my_sv = "hello"sv;
}
Run Code Online (Sandbox Code Playgroud)
C ++ 17或更高版本是否保证以my_sv.data()Null结尾?
===以下为更新===
以下全部来自n4820:
- 根据5.13.5.14,字符串文字是空终止的。
- 根据5.13.8,用户定义的字符串字面量由字符串文字和自定义后缀组成。说,
"hello"sv,hello是字符串文字,sv是后缀。- 按照5.13.8.5,
"hello"sv作为表单的一个呼叫被处理operator "" sv(str, len);为每5.13.5.14,str是空终止。- 按照21.4.2.1,
sv的data()必须返回str。
他们可以证明"hello"sv.data()C ++标准保证它可以为空终止吗?
#include <iostream>
#include <vector>
using namespace std;
typedef int UINT4;
class Hack
{};
Hack& operator <(Hack& a , Hack& b)
{
std::cerr << "1";
return a;
}
Hack& operator >(Hack& a, Hack& b)
{
std::cerr << "2";
return a;
}
int main()
{
Hack vector; // It SHOULD be OK!?
Hack UINT4; // It SHOULD be OK!?
Hack v;
Hack Hack; // It SHOULD be OK!?
Hack = v; // It SHOULD be OK!?
// Please stop to think for …Run Code Online (Sandbox Code Playgroud) #include <iostream>
using namespace std;
struct A
{
A()
{
cout << "A()" << endl;
}
~A()
{
cout << "~A()" << endl;
}
A(A&&)
{
cout << "A(A&&)" << endl;
}
A& operator =(A&&)
{
cout << "A& operator =(A&&)" << endl;
return *this;
}
};
struct B
{
// According to the C++11, the move ctor/assignment operator
// should be implicitly declared and defined. The move ctor
// /assignment operator should implicitly call class A's move
// ctor/assignment …Run Code Online (Sandbox Code Playgroud) 我想用wcout显示混有中文的阿拉伯语消息.
以下代码没问题:
#include <iostream>
using namespace std;
int main()
{
wcout.imbue(locale("chs"));
wcout << L"??"; // OK
}
Run Code Online (Sandbox Code Playgroud)
但是,以下代码不起作用:
#include <iostream>
using namespace std;
int main()
{
wcout.imbue(locale(/* What to place here ??? */));
wcout << L"???????????? ?????????????"; // Output nothing. VC++ 2012 on Win7 x64
// Why does the main advantage of unicode not apply here?
}
Run Code Online (Sandbox Code Playgroud)
我认为在采用unicode之后应该弃用代码页的概念.
Q1.wout显示这样一个文本的机制是什么?
Q2.为什么Windows作为基于unicode的操作系统不支持在其控制台窗口中输出unicode字符?
我有一个类如下:
class VeryVeryVeryLongTypeName
{
bool is_ok;
VeryVeryVeryLongTypeName() : is_ok(false) {}
};
VeryVeryVeryLongTypeName f()
{
VeryVeryVeryLongTypeName v;
... // Doing something
if (condition_1 is true)
{
return v;
}
else
{
return VeryVeryVeryLongTypeName();
}
... // Doing something
if (condition_2 is true)
{
return v;
}
else
{
return VeryVeryVeryLongTypeName();
}
}
Run Code Online (Sandbox Code Playgroud)
我认为这个陈述return VeryVeryVeryLongTypeName();非常乏味和丑陋,所以,我的问题是:
如何优雅地返回默认初始化的对象?
或换句话说:
将功能添加到C++标准中以使以下声明合法是一个好主意吗?
return default; // instead of return VeryVeryVeryLongTypeName();
Run Code Online (Sandbox Code Playgroud) 如何将变量模板函数声明为朋友?
例如如下:
template<class T>
class A
{
friend ??? MakeA ??? ; // What should be placed here ???
A(T)
{}
};
template<class T, class... Args>
A<T> MakeA(Args&&... args)
{
T t(std::forward<Args>(args));
return A(t);
}
Run Code Online (Sandbox Code Playgroud) 以下摘自http://developer.android.com/reference/android/app/KeyguardManager.html
public boolean isDeviceLocked()
返回设备当前是否已锁定并需要PIN,模式或密码才能解锁.如果解锁设备当前需要PIN,模式或密码,则返回true.
public boolean isKeyguardSecure()
返回键盘锁是否需要密码才能解锁.如果keyguard是安全的,则返回true.
isDeviceLocked和之间有什么区别isKeyguardSecure?
#include <sstream>
#include <string>
using namespace std;
template<typename T>
string ToString(const T& obj)
{
ostringstream oss;
oss << obj;
//
// oss will never be used again, so I should
// MOVE its underlying string.
//
// However, below will COPY, rather than MOVE,
// oss' underlying string object!
//
return oss.str();
}
Run Code Online (Sandbox Code Playgroud)
如何移动std :: ostringstream的底层字符串对象?
在cppref,我看到一个奇怪的类型特征检查器:std::has_unique_object_representations
从它的描述,我无法想象任何类型T,使std::has_unique_object_representations<T>::value为false.
有没有反例?
c++ ×9
c++11 ×5
c++17 ×2
standards ×2
type-traits ×2
visual-c++ ×2
android ×1
api ×1
io ×1
keyguard ×1
locking ×1
lockscreen ×1
overloading ×1
performance ×1
return ×1
string ×1
string-view ×1
templates ×1
unicode ×1
widestring ×1
windows-7 ×1