for*_*818 9 c++ templates language-lawyer
这是对成员声明中的模板类类型别名失败替换的跟进
考虑这个代码:
// A
template <typename T>
struct foo {
using type = unsigned;
template <type x>
void bar(type (&)[x]);
};
template <typename T>
template <typename foo<T>::type x>
void foo<T>::bar(type (&)[x]){}
Run Code Online (Sandbox Code Playgroud)
<source>:13:6: error: no declaration matches 'void foo<T>::bar(foo<T>::type (&)[x])'
13 | void foo<T>::bar(type (&)[x]){}
| ^~~~~~
<source>:8:10: note: candidate is: 'template<class T> template<unsigned int x> void foo<T>::bar(foo<T>::type (&)[x])'
8 | void bar(type (&)[x]);
| ^~~
<source>:4:8: note: 'struct foo<T>' defined here
4 | struct foo {
| ^~~
Compiler returned: 1
Run Code Online (Sandbox Code Playgroud)
叮当:
<source>:13:14: error: out-of-line definition of 'bar' does not match any declaration in 'foo<T>'
void foo<T>::bar(type (&)[x]){}
^~~
1 error generated.
Compiler returned: 1
Run Code Online (Sandbox Code Playgroud)
当我删除错误定义和候选人中相同的内容时,我得到了这个:
// B
template <typename T>
struct foo {
using type = unsigned;
template <type x>
void bar();
};
template <typename T>
template <typename foo<T>::type x>
void foo<T>::bar(){}
Run Code Online (Sandbox Code Playgroud)
试图回答原始问题(由 Darhuuk 稍作修改)是这样的:
// C
#include <type_traits>
template <typename T> struct length { using type = unsigned int; };
template <typename T> using length_t = typename length<T>::type;
template <typename type>
class Object {
template <length_t<Object<type>> length>
void put(type (&)[length]);
};
template <typename type>
template <length_t<Object<type>> length>
void Object<type>::put(type (&)[length]) {}
int main() {}
Run Code Online (Sandbox Code Playgroud)
Clang 似乎与原始代码有类似的问题并发出错误:
<source>:15:20: error: out-of-line definition of 'put' does not match any declaration in 'Object<type>'
void Object<type>::put(type (&)[length]) {}
^~~
1 error generated.
Run Code Online (Sandbox Code Playgroud)
谁是对的C?这是clang中的错误还是gcc松懈?
为什么不A编译whileB呢?
| 归档时间: |
|
| 查看次数: |
87 次 |
| 最近记录: |