提问人:Walter 提问时间:1/15/2013 最后编辑:Walter 更新时间:5/11/2013 访问量:5020
未找到模板化中使用表达式的静态 constexpr 成员函数
static constexpr member function in templated using expression not found
问:
对于以下代码
#include <array>
template<unsigned MaxP, typename type>
struct kernel
{
static constexpr unsigned max_pole(unsigned P)
{ return P>MaxP? MaxP:P; }
template<unsigned P>
using array = std::array<type,max_pole(P)>; // wrong?
template<unsigned P>
static void do_something(array<P> const&, array<P>&);
};
GCC 4.7.0 (g++ -c -std=c++11) 给出
error: ‘max_pole’ was not declared in this scope
这是正确的(编译器的行为)吗?请注意,如果我通过将其替换为指示的行来解决,则它可以正常编译。max_pole
kernel::max_pole
编辑已报告给 bugzilla,被接受为 bug c++/55992,参见 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=55992。也发生在 gcc 4.7.x 和 4.8.0 中。
答:
9赞
Andy Prowl
1/15/2013
#1
您的模板在 Clang 3.2 中可以正常编译。我坚信这是一个 GCC 错误(顺便说一句,GCC 4.7.2 中也存在)。GCC 4.8.0 的更改说明似乎没有提到任何此类错误修复。
另请注意,如果删除 的声明,编译错误将消失,这应该不会产生任何影响。do_something<>
还有一个提示:虽然这个模板不能在 GCC 4.7.2 上编译:
template<unsigned MaxP, typename type>
struct kernel
{
static constexpr unsigned max_pole(unsigned P)
{ return P>MaxP? MaxP:P; }
template<typename T>
using array2 = int[max_pole(3)]; // ERROR!
static void do_something(array2<int> const&, array2<int>&);
};
此模板确实编译:
template<unsigned MaxP, typename type>
struct kernel
{
static constexpr unsigned max_pole(unsigned P)
{ return P>MaxP? MaxP:P; }
// template<typename T> <--- removed
using array2 = int[max_pole(3)]; // OK
static void do_something(array2 const&, array2&);
};
由于在这两种情况下都是非限定的独立名称,因此在这两种情况下,查找策略应该是相同的,但事实并非如此。对我来说,这符合一个错误。max_pole
评论
0赞
Gorpik
1/15/2013
因此,根据您的研究,在某些情况下,当您混合两个新的 C++ 功能(+ 别名模板)时,这种情况会特别发生。编译器错误的完美场景。constexpr
0赞
Andy Prowl
1/15/2013
@Gorpik:是的,我也有同样的感觉
0赞
ildjarn
1/17/2013
@Walter : 介意发布链接吗?:-]
0赞
abergmeier
5/4/2013
当您将函数设置为模板时,即使在 clang 3.2 中也会失败。constexpr
评论
g++ 4.7.2