首页 > 解决方案 > 无法在概念上推断占位符类型

问题描述

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

这里有什么问题?

标签: c++c++-conceptsc++20

解决方案


这是由于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>&

推荐阅读