[cfe-dev] clang ignoring global function declaration depending on include/declaration order

Titus von Boxberg titus at v9g.de
Mon Jun 13 02:38:42 PDT 2011


Am 13.06.2011 um 07:17 schrieb Miles Bader:

> Cédric Venet <cedric.venet at laposte.net>
> writes:
>> // this only work on gcc (and most compiler other than clang):
>> template<class T> int bar2(T a) { return a+foo2(); }
>> int foo2();
> 
> This actually _doesn't_ work with any version of g++ I have (4.1 - 4.7):
> 
>   t.cc: In function ‘int bar2(T)’:
>   t.cc:6:49: error: there are no arguments to ‘foo2’ that depend on a template parameter, so a declaration of ‘foo2’ must be available [-fpermissive]
>   t.cc:6:49: note: (if you use ‘-fpermissive’, G++ will accept your code, but allowing the use of an undeclared name is deprecated)

Thanks for the replies!

I managed to forge a sample code that produces my problem.
The following snippet is compileable with g++ but not with clang++.
For clang namespace N has to be dropped, or operator<<
has to be declared before Out<T>.

I have to admit that I currently do not understand this behaviour,
and I was unaware that this declaration order is necessary.

Regards
Titus


// --- start
#include <iostream>

#define	__USENAMESPACENFORB__	1

#if	__USENAMESPACENFORB__
namespace N {
#endif
 class B {
  int b;
 public:
   B(int _b) : b(_b) {}
   int	getB(void) const { return b; }
 };
#if	__USENAMESPACENFORB__
}

using namespace N;
#endif

template <typename T>
  struct Out {
  static void	out(const T& t) {
  std::cout << t << std::endl;
 }
};

// When this operator<< is declared after O::Out it
// does not compile with clang++ if __USENAMESPACENFORB__ is 1
// g++ compiles it in both cases
std::ostream &	operator<<(std::ostream & _str, const B & _b) {
 _str << _b.getB();
 return _str;
}

int main(void) {
 int a = 33;
 B   b(5);
 Out<int>::out(a);
 Out<B>::out(b);
}

// --- end





More information about the cfe-dev mailing list