我正在阅读Constraints上的cppreference页面并注意到这个例子:
// example constraint from the standard library (ranges TS)
template <class T, class U = T>
concept bool Swappable = requires(T t, U u) {
swap(std::forward<T>(t), std::forward<U>(u));
swap(std::forward<U>(u), std::forward<T>(t));
};
Run Code Online (Sandbox Code Playgroud)
我很困惑他们为什么要用std::forward.有些人试图在模板参数中支持引用类型吗?我们不想swap用forward左值调用,并且当标量(非参考)类型时T,表达式不是rvalues U吗?
例如,我希望这个程序在Swappable实现时失败:
#include <utility>
// example constraint from the standard library (ranges TS)
template <class T, class U = T>
concept bool Swappable = requires(T t, U u) {
swap(std::forward<T>(t), std::forward<U>(u));
swap(std::forward<U>(u), std::forward<T>(t)); …Run Code Online (Sandbox Code Playgroud) C++继续让我感到惊讶.今天我发现了 - >*运算符.它是可重载的,但我不知道如何调用它.我设法在课堂上重载它,但我不知道如何调用它.
struct B { int a; };
struct A
{
typedef int (A::*a_func)(void);
B *p;
int a,b,c;
A() { a=0; }
A(int bb) { b=b; c=b; }
int operator + (int a) { return 2; }
int operator ->* (a_func a) { return 99; }
int operator ->* (int a) { return 94; }
int operator * (int a) { return 2; }
B* operator -> () { return p; }
int ff() { return 4; }
}; …Run Code Online (Sandbox Code Playgroud) 鉴于此C++ 11示例代码:
for ( const auto &foo : bar() )
{
// ... do something with foo...
}
Run Code Online (Sandbox Code Playgroud)
标准是否保证bar()本例中的表达式仅被评估一次?
或者它最终会在循环的每次迭代中被调用?
此代码不能使用GCC4.7进行编译
struct A {};
void f(A);
struct B { B(std::tuple<A>); };
void f(B);
int main() {
f(std::make_tuple(A()));
}
Run Code Online (Sandbox Code Playgroud)
因为GCC派生自A利用空基类优化.然而,这导致海湾合作委员会挑选f(A)和抱怨
错误:
'A'是一个无法访问的基础'tuple<A>'
这个错误是由C++标准授予的,还是仅仅是libstdc ++的错误?
以下片段:
#include <functional>
struct X {
X(std::function<double(double)> fn); // (1)
X(double, double); // (2)
template <class T>
auto operator()(T const& t) const { // (3)
return t.foo();
}
};
int main() {
double a, b;
auto x = X(a, b);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
... 当使用- 在OSX和godbolt.org上测试时,无法使用clang(4.0.1)和g++(6.3,7.2)-std=c++14进行编译.
但如果符合以下情况,它编译没有问
(1);double)(3);-> decltype(t.foo()))(3);-std=c++1z(感谢@bolov).也许有一些明显我在这里失踪的东西......这段代码有什么问题吗?这是一个错误吗?
c++ function-object std-function return-type-deduction c++14
我正在考虑使用ICU或Boost Locale.
各自的优点和缺点是什么?
我知道两者都使用ICU,但是ICU被Boost Locale隐藏.根据Boost Locale的基本原理页面:"......整个ICU API隐藏在不透明指针之后,用户无法访问它."
在比较这些库时,请考虑C++ 11中的新Unicode功能.
为什么在C11或C++ 11中没有UTF-8字符文字,即使有UTF-8字符串文字?我理解,一般来说,字符文字表示单个ASCII字符,它与单个八位字节UTF-8代码点相同,但C和C++都没有说编码必须是ASCII.
基本上,如果我读取标准权限,则无法保证'0'将表示整数0x30,但u8"0"必须表示字符序列0x30 0x00.
编辑:
我知道不是每个UTF-8代码点都适合char.这样的文字只对单八位字节代码点(aka,ASCII)有用,所以我猜这称为"ASCII字符文字"会更合适,所以问题仍然存在.我只是选择用UTF-8构建问题,因为有UTF-8字符串文字.我可以想象可以保证ASCII值的唯一方法就是为每个字符写一个常量,考虑到只有128个,这不会那么糟糕,但仍然......
我有以下代码:
#include <string>
#include <vector>
#include <iostream>
class Test final {
public:
Test(const std::string& s)
: s_(s) {
std::cout << "constructing: " << s_ << std::endl;
}
#ifdef NO_MOVE
private:
Test(const Test& t) = delete;
Test(Test&& t) = delete;
#else
public:
Test(const Test& t)
: s_(t.s_) {
std::cout << "copying: " << s_ << std::endl;
};
Test(Test&& t)
: s_(std::move(t.s_)) {
std::cout << "moving: " << s_ << std::endl;
};
#endif
private:
std::string s_;
};
int main() {
std::vector<Test> v; …Run Code Online (Sandbox Code Playgroud) 如果我在VS 2013 Update 2或Update 3中编译此代码:(以下来自Update 3)
#include "stdafx.h"
#include <iostream>
#include <random>
struct Buffer
{
long* data;
int count;
};
#ifndef max
#define max(a,b) (((a) > (b)) ? (a) : (b))
#endif
long Code(long* data, int count)
{
long nMaxY = data[0];
for (int nNode = 0; nNode < count; nNode++)
{
nMaxY = max(data[nNode], nMaxY);
}
return(nMaxY);
}
int _tmain(int argc, _TCHAR* argv[])
{
#ifdef __AVX__
static_assert(false, "AVX should be disabled");
#endif
#ifdef __AVX2__
static_assert(false, "AVX2 should be disabled"); …Run Code Online (Sandbox Code Playgroud) 当我尝试编译时:
#include <functional>
void f(std::function<void()> f)
{
}
void g()
{
f([](auto&&...){});
}
Run Code Online (Sandbox Code Playgroud)
在gcc 7.3上,我收到以下错误:
[x86-64 gcc 7.3#1]错误:无法将'
<lambda closure object>g()::<lambda(auto:1&&, ...)>{}'从'g()::<lambda(auto:1&&, ...)>' 转换为'std::function<void()>'
有人可以解释为什么这是无效的c ++?或者我应该提交错误报告?(MSVC 14接受并按照我的预期编译它.)