小编Ren*_*ter的帖子

如何在无序容器中为用户定义的类型专门化std :: hash <Key> :: operator()?

为了支持用户定义的键类型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,就像编译器和库中的类型一样.咨询后

似乎可以专门化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)

c++ hash unordered-map unordered-set c++11

95
推荐指数
2
解决办法
5万
查看次数

用户定义的文字中是否允许使用C++ 14位分隔符?

当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

42
推荐指数
3
解决办法
2410
查看次数

C++20 中是否允许使用east constexpr / constinit / consteval?

我在网上找到的大多数示例都更喜欢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

c++ c++20

8
推荐指数
1
解决办法
503
查看次数

包含&lt;filesystem&gt;时,MinGW-w64 8.1.0 rev 0无法编译

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)

谁应该为此负责?或更妙的是,该错误要报告给谁?

mingw-w64

7
推荐指数
2
解决办法
1665
查看次数

从初始化列表中推导构造函数的模板参数

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)

c++ language-lawyer template-argument-deduction c++17

3
推荐指数
1
解决办法
594
查看次数