【发布时间】:2017-11-11 08:55:18
【问题描述】:
看看这段代码:
template <typename T, void (T::*pfn)()> struct Testee {};
class Tester
{
private:
void foo() {}
public:
using type_t = Testee<Tester, &Tester::foo>;
};
它使用g++ -std=c++14 -Wall -Wextra成功编译。
但是,当我更改foo 和type_t 的顺序时,出现错误:
$ cat test.cpp
template <typename T, void (T::*pfn)()> struct Testee {};
class Tester
{
public:
using type_t = Testee<Tester, &Tester::foo>;
private:
void foo() {}
};
int main()
{
}
$ g++ -std=c++14 -Wall -Wextra -pedantic test.cpp
test.cpp:6:36: error: incomplete type ‘Tester’ used in nested name specifier
using type_t = Testee<Tester, &Tester::foo>;
^
test.cpp:6:47: error: template argument 2 is invalid
using type_t = Testee<Tester, &Tester::foo>;
^
通常,类定义中的声明顺序对名称解析没有影响。例如:
struct A // OK
{
void foo(int a = val) { }
static constexpr const int val = 42;
};
struct B // OK
{
static constexpr const int val = 42;
void foo(int a = val) { }
};
但是,它在这种情况下会产生影响。为什么?
【问题讨论】:
-
你试过不同的编译器吗?
-
“类定义中的声明顺序没有影响”afaik 这通常不是真的,例如。成员按照声明的顺序构造
-
@tobi303 在这种情况下没有构造(即根本没有调用 ctor)
-
@LucaCappa 我知道,我只是说一般来说这个说法是不正确的
-
@max 嗯,我在coliru 中尝试过clang++,效果一样。
标签: c++ templates member-pointers