c++ - 无法在概念上推断占位符类型
问题描述
我正在尝试使用 GCC 8 中的 Concepts TS 复制标准 C++20 概念,这样我就可以在它们在标准库中可用之前使用它们。我主要是从最新草稿中复制粘贴所有内容,但遇到了一个问题:
#include <type_traits>
#include <utility>
// [concept.same]
template <typename T, typename U>
concept bool Same = std::is_same_v<T, U>;
// [concept.assignable]
// TODO: Proper implementation requires std::common_reference that is not in
// libstdc++ yet and implementing it myself is too hard.
template <typename LHS, typename RHS>
concept bool Assignable = std::is_lvalue_reference_v<LHS> &&
requires(LHS lhs, RHS&& rhs)
{
{lhs = std::forward<RHS>(rhs)} -> Same<LHS>;
};
template <typename T>
requires Assignable<T&, T>
void Test(T a) {}
int main()
{
Test(42);
}
许多其他概念需要可分配的类型,当尝试使用这个概念时,我得到:
Concepts.h:54:14: note: within 'template<class LHS, class RHS> concept const bool ftz::General::Assignable<LHS, RHS> [with LHS = int&; RHS = int]'
concept bool Assignable = std::is_lvalue_reference_v<LHS> &&
^~~~~~~~~~
Concepts.h:54:14: note: with 'int& lhs'
Concepts.h:54:14: note: with 'int&& rhs'
Concepts.h:54:14: note: unable to deduce placeholder type 'ftz::General::Same<int&>' from 'lhs =(forward<int>)(rhs)'
这里有什么问题?
解决方案
这是由于P1084而在圣地亚哥(2018 年 11 月)采用的概念的最新更改。问题是它曾经是这样的:
{ E } -> Same<T>;
实际上意味着该表达式f(E)
对于以下形式的发明函数模板有效:
template <class U> requires Same<U, T> void f(U );
这显然永远不会适用于引用类型T
(如在 OP 中)。
换句话说,旧规则是:{ E } -> Same<T>
意思是Same<remove_cvref_t<decltype((E))>, T>
。新规则是它的意思Same<decltype((E)), T>
。似乎 gcc-fconcepts
和 clang 的概念分支都没有实施这些新规则。
当前的解决方法是更改:
{ E } -> Same<LHS> // i.e. Same<T&>
至:
{ E } -> Same<std::remove_reference_t<LHS>>& // i.e. Same<T>&
推荐阅读
- javascript - 结合 settimeout 和 label Loop 和 if 语句
- javascript - 如果选中复选框,我如何在复选框旁边的文本中运行一行?
- python - 从python中的excel文件名中提取日期
- python - 熊猫数据框根据条件替换值
- flutter - 如何在颤动中获取歌曲的封面图片(专辑封面)?
- python - ValueError:无法将字符串转换为浮点数:'W'
- json - 将字符串文字添加到 JSONPath 输出中
- flutter - 查看 mainAxisSize 效果的最低设置是多少?
- react-native - 如何在 react-native 的不同屏幕上以编程方式设置 windowSoftInputMode?
- html - 想用引导导航栏制作背景png,但它不起作用