The code below builds fine with any version of GCC 4.x ######## #include <iostream> void Test(std::ostream &stream) { stream << "Hello, World!" << std::endl; } int main() { Test(std::cout); return 0; } ######## But GCC 4.2.1, 4.4.0, 4.6.4 and 4.7.3 refuse to build the below code while VC++10, VC++11 and both CLANG 3.0 and 3.1 are building it without any issue or warning. [ I got it from here: http://stackoverflow.com/questions/8629382/debug-macro-for-c-with-variable-arguments-without-the-format-string ] ######## #include <iostream> #include <string> class VariadicToOutputStream { public: VariadicToOutputStream(std::ostream& s, const std::string& separator = " ") : m_stream(s), m_hasEntries(false), m_separator(separator) {} template<typename ObjectType> VariadicToOutputStream& operator , (const ObjectType& v) { if (m_hasEntries) m_stream << m_separator; m_stream << v; m_hasEntries=true; return *this; } ~VariadicToOutputStream() { m_stream << std::endl; } private: std::ostream& m_stream; bool m_hasEntries; std::string m_separator; }; #define VARIADIC_TO_STDOUT(...) \ VariadicToOutputStream(std::cout),__VA_ARGS__; int main() { VARIADIC_TO_STDOUT(1, 0.5f, "a string"); return 0; } ######## The error produced by GCC: ######## main.cpp: In function 'int main()': main.cpp:31:2: error: invalid use of qualified-name 'std::cout' main.cpp:31:2: error: expected unqualified-id before numeric constant ######## Versions of GCC that I've used: g++ (GCC) 4.2.1 20070831 patched [FreeBSD] g++46 (FreeBSD Ports Collection) 4.6.4 20120928 (prerelease) g++47 (FreeBSD Ports Collection) 4.7.3 20120929 (prerelease) MinGW g++ (GCC) 4.4.0
#include <iostream> struct A { A(std::ostream& s); }; int main() { A(std::cout); }
Looks like GCC is getting confused if the constructor is going to be a function declaration or not.
(In reply to comment #2) > Looks like GCC is getting confused if the constructor is going to be a function > declaration or not. s/function/variable/ Here is a testcase without the include: struct f{}; namespace s{ f g; } struct A { A(f& s); }; int main() { A(f::g); }
Comeau rejects it too: Comeau C/C++ 4.3.10.1 (Oct 6 2008 11:28:09) for ONLINE_EVALUATION_BETA2 Copyright 1988-2008 Comeau Computing. All rights reserved. MODE:strict errors C++ C++0x_extensions "ComeauTest.c", line 12: error: qualified name is not allowed A(f::g); ^ So I almost want to say GCC is correct.
The real testcase that shows the issue with GCC: struct f { }; f g; struct A { A(f& s); }; int main() { A(g), 1; } --- CUT --- The other testcase I think was invalid, the one above I think is valid code and should not be rejected.
Dup of bug 29834 *** This bug has been marked as a duplicate of bug 29834 ***