Constexpr 函数指针作为模板参数 MSVC vs GCC
Constexpr function pointer as template argument MSVC vs GCC
我有一些代码可以使用 msvc 编译,但不能在 gcc 下编译。我想知道这是 msvc 的非标准功能还是 gcc 中的错误(或者更准确地说是 MinGW 的 v5.0.3 版本)
考虑以下代码:
template <class T>
struct object_with_func_ptr {
const T func; // An object to hold a const function pointer.
};
class foo {
public:
void bar() {} // The function I want to point to.
static constexpr auto get_bar() {
return object_with_func_ptr<decltype(&bar)>{&bar}; // A constexpr to wrap a function pointer.
}
};
template <class Func, Func Fn> struct template_using_func {};
int main() {
constexpr auto bar_obj = foo::get_bar();
// Note that this is a constexpr variable and compiles for gcc also if the template_using_func line is left out.
constexpr auto bar_func_ptr = bar_obj.func;
template_using_func<decltype(bar_func_ptr), bar_func_ptr>();
return 0;
}
如果这是 msvc 的非标准功能,那么知道是否有其他方法可以实现我的目标会很棒。
编辑:
这里是 MinGW 生成的编译器错误:
E:\CLion\ErrorExample\main.cpp: In function 'int main()':
E:\CLion\ErrorExample\main.cpp:21:61: error: 'bar_func_ptr' is not a valid template argument for type 'void (foo::* const)()'
template_using_func<decltype(bar_func_ptr), bar_func_ptr>();
^
E:\CLion\ErrorExample\main.cpp:21:61: error: it must be a pointer-to-member of the form '&X::Y'
E:\CLion\ErrorExample\main.cpp:21:61: error: could not convert template argument 'bar_func_ptr' from 'void (foo::* const)()' to 'void (foo::* const)()'
编辑 2:
将 &bar
更改为 &foo::bar
显然也会为 clang 编译(如评论中所述),所以我目前假设这是 GCC 中的错误。
嗯,有一些明显的错误,比如 &bar
而不是 &foo::bar
。如果我们解决这些问题,Clang agrees with MSVC,并且当 3 个主要编译器中有 2 个同意并且它有道理时,我会选择有道理的大多数人。
我在 gcc 上测试时的错误是 void (foo::* const)()' to 'void (foo::* const)()
这让我认为这是一个错误。现在,可能是对来自某些值类别的非类型模板参数的一些模糊要求在这里没有得到满足,但我希望那时会有更清晰的错误消息。
顺便说一句,std::integral_constant
can do a better job 表示编译时函数指针而不是 class,除非您真的希望运行时能够更改指针指向的位置。
在 C++17 之前,标准将非空指向成员的指针模板参数限制为
"a pointer to member expressed as described in [expr.unary.op]"。换句话说,这样的参数必须以 &A::B
的确切形式表达。
C++17 放宽了此约束以允许通用常量表达式。这似乎还没有在 GCC 中实现。
我有一些代码可以使用 msvc 编译,但不能在 gcc 下编译。我想知道这是 msvc 的非标准功能还是 gcc 中的错误(或者更准确地说是 MinGW 的 v5.0.3 版本)
考虑以下代码:
template <class T>
struct object_with_func_ptr {
const T func; // An object to hold a const function pointer.
};
class foo {
public:
void bar() {} // The function I want to point to.
static constexpr auto get_bar() {
return object_with_func_ptr<decltype(&bar)>{&bar}; // A constexpr to wrap a function pointer.
}
};
template <class Func, Func Fn> struct template_using_func {};
int main() {
constexpr auto bar_obj = foo::get_bar();
// Note that this is a constexpr variable and compiles for gcc also if the template_using_func line is left out.
constexpr auto bar_func_ptr = bar_obj.func;
template_using_func<decltype(bar_func_ptr), bar_func_ptr>();
return 0;
}
如果这是 msvc 的非标准功能,那么知道是否有其他方法可以实现我的目标会很棒。
编辑: 这里是 MinGW 生成的编译器错误:
E:\CLion\ErrorExample\main.cpp: In function 'int main()':
E:\CLion\ErrorExample\main.cpp:21:61: error: 'bar_func_ptr' is not a valid template argument for type 'void (foo::* const)()'
template_using_func<decltype(bar_func_ptr), bar_func_ptr>();
^
E:\CLion\ErrorExample\main.cpp:21:61: error: it must be a pointer-to-member of the form '&X::Y'
E:\CLion\ErrorExample\main.cpp:21:61: error: could not convert template argument 'bar_func_ptr' from 'void (foo::* const)()' to 'void (foo::* const)()'
编辑 2:
将 &bar
更改为 &foo::bar
显然也会为 clang 编译(如评论中所述),所以我目前假设这是 GCC 中的错误。
嗯,有一些明显的错误,比如 &bar
而不是 &foo::bar
。如果我们解决这些问题,Clang agrees with MSVC,并且当 3 个主要编译器中有 2 个同意并且它有道理时,我会选择有道理的大多数人。
我在 gcc 上测试时的错误是 void (foo::* const)()' to 'void (foo::* const)()
这让我认为这是一个错误。现在,可能是对来自某些值类别的非类型模板参数的一些模糊要求在这里没有得到满足,但我希望那时会有更清晰的错误消息。
顺便说一句,std::integral_constant
can do a better job 表示编译时函数指针而不是 class,除非您真的希望运行时能够更改指针指向的位置。
在 C++17 之前,标准将非空指向成员的指针模板参数限制为
"a pointer to member expressed as described in [expr.unary.op]"。换句话说,这样的参数必须以 &A::B
的确切形式表达。
C++17 放宽了此约束以允许通用常量表达式。这似乎还没有在 GCC 中实现。