缩写函数模板。“Constraint auto const&”类型有效,但“Constraint const auto&”失败

Joh*_*itb 5 c++ templates constraints c++20

对于 boost 容器哈希,我需要提供 ADL-reachable hash_value。我想禁用隐式转换,因此我将其编写为模板,强制参数为类类型。

inline auto hash_value(std::same_as<ClassType> const auto& key) -> std::size_t;
Run Code Online (Sandbox Code Playgroud)

const令我惊讶的是,它失败了,但如果我交换和的顺序就可以了auto

为了建立更深入的理解:我如何从概念上解释这种情况?const auto&到目前为止我认为该参数有一个占位符类型,并给出了一个表示约束的说明符。但这似乎是一种情况,其中序列constraint auto是专门检测的语法,如果我重新排序类型说明符,该机制就会失败。

Sto*_*ica 10

如果我重新排序类型说明符,机制就会失败

正确的。因为约束本身并不是类型说明符。从语法上讲,它是占位符类型本身的一部分

占位符类型说明符:
    类型约束opt auto
    类型约束opt decltype ( auto )

那里没有空间容纳其他说明符。相反,这确实有效

inline auto hash_value(const std::same_as<ClassType> auto& key) -> std::size_t;
Run Code Online (Sandbox Code Playgroud)

如果你处于错误的左阵营,而不是右阵营。