通过概念启用的成员函数的外部类别定义

发布于 2025-02-13 16:56:00 字数 1323 浏览 0 评论 0 原文

以下这个问题,我试图定义成员函数由班级定义之外的概念启用:

template<typename T>
concept MyConcept = std::is_integral<T>::value; //could be anything...

template<typename T>
class F {
public:
    void foo() requires MyConcept<F>;
};

template<typename T>
void F<T>::foo() requires MyConcept<F<T>>
{
    //long implementation...
}

此代码在GCC上正常工作,但在MSVC和Clang上失败。

请注意,我需要在 f&lt; t&gt; 上满足该概念,而不仅仅是 t (我的实际代码更为复杂,并且使用variadic模板)。

demo

clang clang说:

error: out-of-line definition of 'foo' does not match any declaration in 'F<T>'
void F<T>::foo() requires MyConcept<F<T>>
           ^~~

note: member declaration nearly matches
void foo() requires MyConcept<F>;
     ^

如果我在班级中移动定义,在clang上可以正常工作:

template<typename T>
class F {
public:
    void foo() requires MyConcept<F> 
    {
        //long implementation...
    }
};

无论如何,我想避免在班级定义中进行长时间的实现。

我在做什么错? 如何正确定义启用班级概念的成员函数?

Following this question, I was trying to define a member function enabled by a concept outside the class definition:

template<typename T>
concept MyConcept = std::is_integral<T>::value; //could be anything...

template<typename T>
class F {
public:
    void foo() requires MyConcept<F>;
};

template<typename T>
void F<T>::foo() requires MyConcept<F<T>>
{
    //long implementation...
}

this piece of code works fine on GCC, but fails on MSVC and Clang.

Note that I need that the concept must be satisfied on F<T>, and not only T (my actual code is much more complex and uses variadic templates).

Demo

Clang says:

error: out-of-line definition of 'foo' does not match any declaration in 'F<T>'
void F<T>::foo() requires MyConcept<F<T>>
           ^~~

note: member declaration nearly matches
void foo() requires MyConcept<F>;
     ^

It works fine on Clang if I move the definition inside the class, but not on msvc:

template<typename T>
class F {
public:
    void foo() requires MyConcept<F> 
    {
        //long implementation...
    }
};

In any case, I would like to avoid having long implementations inside class definitions.

What am I doing wrong?
How can I define member function enabled with a concept outside the class correctly?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

醉生梦死 2025-02-20 16:56:00

这是一个编译器错误。

  • 它已在Clang版本16上解决;
  • 它仍然无法解决Apple Clang。
  • 它似乎已在MSVC版本19.36.32532.0上解决,尽管它仍在“考虑” 在这里。

请注意,对于MSVC 19.36.32532.0,在函数定义中,需要子句在声明中必须是相同的( myConcept&lt; f&gt; and and myconcept&concept ; t&gt;&gt; ):

template<typename T>
void F<T>::foo() requires MyConcept<F>
{
    //long implementation...
}

It is a compiler bug.

  • It has been solved on Clang version 16;
  • It is still not solved on Apple Clang;
  • It seems to be solved on MSVC version 19.36.32532.0, although it is still "under consideration" here.

Note that, for MSVC 19.36.32532.0, in the function definition the requires clause must be the same of the declaration (MyConcept<F> and not MyConcept<F<T>>):

template<typename T>
void F<T>::foo() requires MyConcept<F>
{
    //long implementation...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文