gpt4 book ai didi

c++ - 无法在概念中推断占位符类型

转载 作者:太空狗 更新时间:2023-10-29 20:50:19 25 4
gpt4 key购买 nike

我正在尝试使用 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)'

这里有什么问题?

最佳答案

这是圣地亚哥(2018 年 11 月)由于 P1084 对概念所做的最新更改。 .问题是它曾经是:

{ 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>&

关于c++ - 无法在概念中推断占位符类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55198202/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com