为了支持用户定义的键类型std::unordered_set<Key>和std::unordered_map<Key, Value>
一个具有提供operator==(Key, Key)和散列函子:
struct X { int id; /* ... */ };
bool operator==(X a, X b) { return a.id == b.id; }
struct MyHash {
size_t operator()(const X& x) const { return std::hash<int>()(x.id); }
};
std::unordered_set<X, MyHash> s;
Run Code Online (Sandbox Code Playgroud)
std::unordered_set<X>
使用类型的默认哈希来编写会更方便X,就像编译器和库中的类型一样.咨询后
include\c++\4.7.0\bits\functional_hash.h include\xfunctional似乎可以专门化std::hash<X>::operator():
namespace std { // argh!
template <>
inline size_t
hash<X>::operator()(const X& x) const { return …Run Code Online (Sandbox Code Playgroud) 当clang编译以下行时,g ++ 6.1会抱怨数字分隔符(请参阅Coliru上的实例):
auto time = 01'23s;
Run Code Online (Sandbox Code Playgroud)
根据C++ 14标准(N3796),哪个编译器(如果有)是正确的?
否则,允许数字分隔符(§2.14.2)只是<chrono>库的用户定义文字(§2.14.8)中的实现细节(§20.12.5.8)?恕我直言,它应该不是,因为这些文字是在unsigned long long参数上定义的.
我记得Howard Hinnant10'000s在他的CppCon 2016演讲"A <chrono>tutorial"(在他的演讲中大约42分钟)中作为一个例子.
(请注意,我不打算编码"1分23秒",这只是偶然的,因为八进制文字0123是64 + 16 + 3 == 83.为此我应该写
auto time = 1min + 23s;
Run Code Online (Sandbox Code Playgroud)
但是这种可能误导性的解释不是问题的一部分.)
c++ language-lawyer user-defined-literals c++14 digit-separator
我在网上找到的大多数示例都更喜欢constexpr(C++11) consteval、 和constinit(C++20)的“西方风格” :
consteval auto sqr(int n) {
return n*n;
}
constexpr auto r = sqr(100); // OK
constinit static auto x = r;
Run Code Online (Sandbox Code Playgroud)
由于我不是语言律师,我有以下问题:这些说明符是否允许使用“东方风格”?例子:
auto consteval sqr(int n) {
return n*n;
}
auto constexpr r = sqr(100); // OK
static auto constinit x = r;
Run Code Online (Sandbox Code Playgroud)
需要明确的是:我无意发动“西方”/“东方”语言战争。我不是在意见感兴趣,只是事实,尤其是在wandbox铛和gcc版本头在这个时候给出错误但没有答案constinit/ consteval。
2天前,我很高兴地注意到MinGW-w64发布了其gcc 8.1.0,修订版0。不幸的是,这是一个简单的程序
#include <filesystem>
int main() {}
Run Code Online (Sandbox Code Playgroud)
不编译。它导致内部出现许多错误<filesystem>,从
C:/MinGW/mingw32/lib/gcc/i686-w64-mingw32/8.1.0/include/c++/bits/fs_path.h: In member function 'std::filesystem::__cxx11::path& std::filesystem::__cxx11::path::operator/=(const std::filesystem::__cxx11::path&)':
C:/MinGW/mingw32/lib/gcc/i686-w64-mingw32/8.1.0/include/c++/bits/fs_path.h:237:47: error: no match for 'operator!=' (operand types are 'std::filesystem::__cxx11::path' and 'std::filesystem::__cxx11::path')
|| (__p.has_root_name() && __p.root_name() != root_name()))
~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~
Run Code Online (Sandbox Code Playgroud)
谁应该为此负责?或更妙的是,该错误要报告给谁?
在Kona会议上,构造函数的模板参数推导(P0091R0)已获得批准。它简化了一些变量定义:
std::pair p {1,2}; // o.k., constructor pair<int,int>(1,2)
std::vector v1 (10, 0); // o.k., 10 zeroes, constructor vector<int>(size_t n, T initvalue)
std::vector v2 {10, 0}; // o.k., 2 values: 10, 0, apparently initializer list?
std::vector v3 = {10, 0}; // o.k., same as v2?
Run Code Online (Sandbox Code Playgroud)
但是,以下行不会在 gcc 7 HEAD 201611 版本中编译(实时示例):
std::vector v4 = {3}; // error: no matching function for call to 'std::vector(int)'
std::vector v5 {1, 2, 3}; // error: 'int' is not a …Run Code Online (Sandbox Code Playgroud)