考虑:
struct Person
{
int height;
int weight;
int age;
};
int main()
{
Person p { .age = 18 };
}
Run Code Online (Sandbox Code Playgroud)
上面的代码在C99中是合法的,但在C++ 11中不合法.
什么是C++ 11标准委员会排除了这样一个方便的功能支持的理由?
一个简短的例子输出一个奇怪的结果!
#include <iostream>
using namespace std;
struct A { int a; };
struct B { int b; };
struct C : A, B
{
int c;
};
int main()
{
C* c = new C;
B* b = c;
cout << "The address of b is 0x" << hex << b << endl;
cout << "The address of c is 0x" << hex << c << endl;
if (b == c)
{
cout << "b is equal to c" << …
Run Code Online (Sandbox Code Playgroud) 我正在用C++编写一个控制台程序来下载一个大文件.我知道文件大小,我开始下载工作线程.我想显示一个进度指示器,使其看起来更酷.
如何在cout或printf中的不同时间,但在同一位置显示不同的字符串?
template<typename T>
void f(T a, const T& b)
{
++a; // ok
++b; // also ok!
}
template<typename T>
void g(T n)
{
f<T>(n, n);
}
int main()
{
int n{};
g<int&>(n);
}
Run Code Online (Sandbox Code Playgroud)
请注意:b
是的const T&
,++b
没关系!
为什么const T&
不确定是const?
在http://blogs.msdn.com/b/vcblog/archive/2011/09/12/10209291.aspx上,VC++团队正式声明他们尚未实现C++ 11核心功能"Expression SFINAE".但是,从http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2634.html复制的以下代码示例将被VC++编译器接受.
例1:
template <int I> struct A {};
char xxx(int);
char xxx(float);
template <class T> A<sizeof(xxx((T)0))> f(T){}
int main()
{
f(1);
}
Run Code Online (Sandbox Code Playgroud)
例2:
struct X {};
struct Y
{
Y(X){}
};
template <class T> auto f(T t1, T t2) -> decltype(t1 + t2); // #1
X f(Y, Y); // #2
X x1, x2;
X x3 = f(x1, x2); // deduction fails on #1 (cannot add X+X), calls #2
Run Code Online (Sandbox Code Playgroud)
我的问题是:什么是"表达SFINAE"?
#include <iostream>
using namespace std;
int main()
{
char c1 = 0xab;
signed char c2 = 0xcd;
unsigned char c3 = 0xef;
cout << hex;
cout << c1 << endl;
cout << c2 << endl;
cout << c3 << endl;
}
Run Code Online (Sandbox Code Playgroud)
我预计输出如下:
ab
cd
ef
Run Code Online (Sandbox Code Playgroud)
然而,我一无所获.
我想这是因为cout总是将'char','signed char'和'unsigned char'视为字符而不是8位整数.但是,'char','signed char'和'unsigned char'都是完整的类型.
所以我的问题是:如何通过cout将字符输出为整数?
PS:static_cast(...)很难看,需要更多的工作来修剪额外的比特.
#include <compare>
struct A
{
int n;
auto operator<=>(A const& other) const
{
if (n < other.n)
{
return std::strong_ordering::less;
}
else if (n > other.n)
{
return std::strong_ordering::greater;
}
else
{
return std::strong_ordering::equal;
}
}
// compile error if the following code is commented out.
// bool operator==(A const& other) const
// { return n == other.n; }
};
int main()
{
A{} == A{};
}
Run Code Online (Sandbox Code Playgroud)
看在线演示
为什么我必须 operator ==
在 足够的时候提供operator <=>
?
c++ language-design language-lawyer spaceship-operator c++20
#include <iostream>
using namespace std;
struct A
{
A() { cout << "A" << endl; }
~A() { cout << "~A" << endl; }
};
A Ok() { return {}; }
A NotOk() { throw "NotOk"; }
struct B
{
A a1;
A a2;
};
void f(B) {}
int main()
{
try
{
f({ Ok(), NotOk() });
}
catch (...)
{}
}
Run Code Online (Sandbox Code Playgroud)
vc++
并clang
输出:
A
~A
Run Code Online (Sandbox Code Playgroud)
虽然gcc
产出:
A
Run Code Online (Sandbox Code Playgroud)
这似乎是GCC的一个严重错误.
struct A
{
A();
A(const A&);
A& operator =(const A&);
A(A&&) = delete;
A& operator =(A&&) = delete;
};
struct B
{
B();
B(const B&);
B& operator =(const B&);
};
int main()
{
A a;
a = A(); // error C2280
B b;
b = B(); // OK
}
Run Code Online (Sandbox Code Playgroud)
我的编译器是VC++ 2013 RC.
错误C2280:'A&A :: operator =(A &&)':尝试引用已删除的函数
我只是想知道为什么编译器A& operator =(const A&);
在A& operator =(A&&)
删除时不会尝试?
这种行为是由C++标准定义的吗?
int main()
{
char c = 0xff;
bool b = 0xff == c;
// Under most C/C++ compilers' default options, b is FALSE!!!
}
Run Code Online (Sandbox Code Playgroud)
C或C++标准都没有将char指定为有符号或无符号,它是实现定义的.
为什么C/C++标准没有明确地将char定义为有符号或无符号以避免像上述代码那样的危险误用?