c++ - 默认模板类参数混淆了 g++?

标签 c++ templates gcc g++ intel

昨天我遇到了一个 g++ (3.4.6) 编译器问题,我使用 Intel (9.0) 编译器编译的代码没有问题。这是显示所发生情况的代码片段:

template<typename A, typename B>
class Foo { };

struct Bar {
   void method ( Foo<int,int> const& stuff = Foo<int,int>() );
};

g++ 编译器错误是:

foo.cpp:5: error: expected `,' or `...' before '>' token
foo.cpp:5: error: wrong number of template arguments (1, should be 2)
foo.cpp:2: error: provided for `template<class A, class B> struct Foo'
foo.cpp:5: error: default argument missing for parameter 2 of `void Bar::method(const Foo<int, int>&, int)'

显然,以这种方式编写时不接受默认参数,并且编译器假设指定了一个新的函数参数而不是第二个模板参数,然后它期望一个默认值,因为 stuff 参数有一个。我可以通过创建一个 typedef 来帮助编译器,然后一切都可以正常编译:

template<typename A, typename B>
class Foo { };

struct Bar {
   typedef Foo<int,int> FooType;
   void method ( FooType const& stuff = FooType() );
};

所以我可以解决我的问题,但我不明白发生了什么。我在这里错过了 C++(模板?)语言功能,我做错了什么,还是 g++ 编译器不接受第一段代码是错误的?

请注意顺便说一句,这也可以编译......

template<typename A, typename B>
class Foo { };

void method ( Foo<int,int> const& stuff = Foo<int,int>() );

最佳答案

我不太确定这是 g++(自版本 4.2.4 起)中的错误。该代码现在通过了 g++ 4.4(请参阅下面的更新)。为了让此代码为其他版本的编译器编译,您可以在默认参数周围添加一组括号:

template<typename A, typename B>
class Foo { };

struct Bar {
  void method ( Foo<int,int> const& stuff = ( Foo<int,int>() ) );
};

IMO,这些括号是必要的,因为还有一个额外的要求,即默认参数可以引用类的成员,该成员可能稍后在类主体中声明:

struct Bar {
  void method ( int i = j);  // 'j' not declared yet
  static const int j = 0;
};

上面的代码是合法的,在解析'method'的声明时还没有看到成员'j'。因此,编译器只能使用语法 检查(即匹配括号和逗号)来解析默认参数。当 g++ 解析您的原始声明时,它实际看到的是以下内容:

void method ( Foo<int,int> const& stuff = Foo<int // Arg 1 with dflt.
              , int>() );                         // Arg 2 - syntax error

添加额外的一组括号可确保正确处理默认参数。

以下情况显示了 g++ 成功但 Comeau 仍然生成语法错误的示例:

template<int J, int K>
class Foo { };

struct Bar {
  void method ( Foo<0, 0> const & i = ( Foo<j, k> () ) );
  static const int j = 0;
  static const int k = 0;
};

编辑:

作为对评论的回应:“你也可以在那里进行带有多个参数的函数调用”,这不会导致问题的原因是函数调用中的逗号被括在括号中:

int foo (int, int, int);
struct Bar {
  void method ( int j =
                    foo (0, 0, 0) ); // Comma's here are inside ( )
};

因此,可以仅使用表达式的语法 来解析它。在 C++ 中,所有的 '(' 必须与 ')' 匹配,所以这很容易解析。这里出现问题的原因是“<”不需要匹配,因为它在 C++ 中被重载,因此可以是小于运算符或模板参数列表的开头。以下示例显示了默认参数中使用“<”的位置,并暗示小于运算符:

template<int I, int J>
class Foo { };

struct Bar {
  template <typename T> struct Y { };

  void method ( ::Foo<0,0> const& stuff = Foo<10 , Y < int >  = Y<int>() );

  struct X {
    ::Foo<0, 0> operator< (int);
  };

  static X Foo;
};

上面的 "Foo<10"是对 'X' 中定义的 "operator<"的调用,而不是模板参数列表的开头。同样,Comeau 在上述代码上生成了语法错误,而 g++(包括 3.2.3)正确地解析了它。

仅供引用,适当的引用是 8.3.6/5 中的注释:

[Note: in member function declarations, names in default argument expressions are looked up as described in 3.4.1...

然后在 3.4.1/8

A name used in the definition of a member function (9.3) of class X following the function’s declaratorid29) shall be declared in one of the following ways:

...

— shall be a member of class X or be a member of a base class of X (10.2), or

此处的项目符号是强制编译器“延迟”查找默认参数含义的部分,直到声明所有类成员为止。

<更新>

正如“Employed Russian”所指出的,g++ 4.4 现在能够解析所有这些示例。然而,直到 DR已由 C++ 标准委员会解决,我还没有准备好将其称为“错误”。我相信需要长期的额外括号以确保对其他编译器/工具(甚至可能是 g++ 的 future 版本)的可移植性。

根据我的经验,C++ 标准并未规定编译器供应商都应使用相同的解析器技术,而且他们也不能期望所有技术都同样强大。因此,解析需求通常不需要供应商执行超人的壮举。为了说明这一点,请考虑以下两个示例:

typedef T::TYPE TYPE;
T::TYPE t;

如果“T”是相关的,则给定每个上下文“TYPE”必须是一个类型名称,但是标准仍然需要类型名称 关键字。这些示例是明确的,只能表示一件事,但是标准(为了允许所有解析器技术)仍然需要 typename 关键字。

只要额外的括号允许代码解析,未能解析这些示例的编译器仍然是“符合标准的”,DR 可能会以这样一种方式被解决。

关于c++ - 默认模板类参数混淆了 g++?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23061912/

相关文章:

c++ - 关于-Waggregate-return 的目的/行为的混淆?

c++ - 如何在 C++ 程序中查看 gdb 中的 vtable?

c - 如果变量不在函数的顶部,则禁用变量声明

c++ - 应用在 RPC 上挂起

c++ - 为什么我的C++程序崩溃了。我是圆括号的实现问题

c++ - 为什么模板实参用作另一个模板的模板参数时不能推导出来?

c++ - 我可以在 C++ 的模板函数体中使用模板变量吗

c++ - 我需要一个用于 boost::ublas 矩阵的虚拟析构函数吗?

c++ - 类中的字符串//C++

C++ 模板类型名迭代器