简体   繁体   English

clang 是否有权抱怨使用概念的模板化构造函数的定义异常?

[英]Is clang right to complain about an out of line definition of a templated constructor using a concept?

Here is a struct with a templated constructor that is defined out of line:这是一个带有模板化构造函数的结构,该构造函数被定义为异常:

template <typename T>
struct Foo {
  template <typename F>
  Foo(F f);
};

template <typename T>
template <typename F>
Foo<T>::Foo(F f) {}

Clang is happy with this under -std=c++20 . Clang对此感到满意-std=c++20 If I add a requires clause to the templated constructor, it is still happy.如果我向模板化构造函数添加一个requires子句,它仍然很高兴。 But if the requires clause mentions the struct, it is not happy :但是如果requires子句提到结构,它就不高兴了:

#include <concepts>

template <typename T>
struct Foo {
  template <typename F>
    requires (!std::same_as<Foo<T>, int>)
  Foo(F f);
};

template <typename T>
template <typename F>
  requires (!std::same_as<Foo<T>, int>)
Foo<T>::Foo(F f) {}
<source>:13:9: error: out-of-line definition of 'Foo<T>' does not match any declaration in 'Foo<T>'
Foo<T>::Foo(F f) {}
        ^~~

GCC does accept this . GCC确实接受了这个

Is clang right to reject it? clang拒绝就对了吗? And if so, what part of the standard says so?如果是这样,标准的哪一部分是这样说的?


If you're interested why I have a requires clause that references the type being constructed, it's to disambiguate the constructor from the move constructor so that the next requires clause in a larger requires expression won't be evaluated when F is the same as Foo .如果您对为什么我有一个引用正在构造的类型的requires子句感兴趣,这是为了消除构造函数与移动构造函数的歧义,以便当FFoo相同时,不会评估更大的requires表达式中的下一个requires子句. Otherwise it recursively depends upon itself.否则它递归地依赖于它自己。 The real code is more complicated, and accepts a forwarding reference to F .实际代码更复杂,并接受对F的转发引用。

I think this could be related to the compiler not being able to deduce what T is.我认为这可能与编译器无法推断出 T 是什么有关。 When using a requires clause inside a struct, the compiler needs to be able to instantiate the class template in order to check the requirements specified in the requires clause.在结构中使用 requires 子句时,编译器需要能够实例化 class 模板,以便检查 requires 子句中指定的要求。 This means that all template parameters used within the requires clause must be in scope and the compiler must be able to deduce their types.这意味着 requires 子句中使用的所有模板参数必须在 scope 中,并且编译器必须能够推断出它们的类型。 If the class template is not fully defined yet, the compiler will not be able to instantiate it, and the code will not be able to compile.如果class模板还没有完全定义,编译器将无法实例化它,代码将无法编译。 Just to be clear, I do not know with certainty, as I have barely used this feature.需要明确的是,我不确定,因为我几乎没有使用过这个功能。

As I sort of expected, this seems to be a known clang bug: https://github.com/llvm/llvm-project/issues/49620正如我所料,这似乎是一个已知的 clang 错误: https://github.com/llvm/llvm-project/issues/49620

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C++ 概念/SFINAE:clang 和 MSVC/G++ 不同的结果,用于离线 function 定义与模板和概念/SFINAE - C++ Concepts/SFINAE: clang and MSVC/G++ different results for out of line function definition with template and Concept/SFINAE 使用模板化构造函数解释为函数定义的非模板化类的实例? - Instance of a non-templated class using templated constructor interpreted as a function definition? 为什么我的编译器会抱怨一个概念缺少模板参数? - Why does my compiler complain about a missing template argument for a concept? 离线构造函数模板在GCC中无法在Clang中运行 - out-of-line constructor template works in GCC fails in Clang 为什么编译器没有在Derived类构造函数的定义中抱怨? - Why doesn't the compiler complain in the definition of the Derived class constructor? 引导构造函数的异常定义(c ++ 20) - out of line definition of guided constructor (c++20) 模板化函数重载和类定义外的错误 - Error with templated function overloading and out of class definition "对于基类中的模板化构造函数,clang\/gcc 和 MSVC 之间的结果不同" - Different results between clang/gcc and MSVC for templated constructor in base class 使用SFINAE作为模板化构造函数时遇到麻烦 - trouble using SFINAE for templated constructor 关于静态模板化constexpr的clang警告(未定义内联函数) - Clang warning about static templated constexpr (inline function is not defined)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM