以下这个问题,我试图定义成员函数由班级定义之外的概念启用:
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?
发布评论
评论(1)
这是一个编译器错误。
请注意,对于MSVC 19.36.32532.0,在函数定义中,
需要
子句在声明中必须是相同的(myConcept&lt; f&gt;
and andmyconcept&concept ; t&gt;&gt;
):It is a compiler bug.
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 notMyConcept<F<T>>
):