Bug 54905 - invalid use of qualified-name 'std::cout'
Summary: invalid use of qualified-name 'std::cout'
Status: RESOLVED DUPLICATE of bug 29834
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.6.4
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2012-10-11 20:35 UTC by M.S. Babaei
Modified: 2012-10-11 21:54 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description M.S. Babaei 2012-10-11 20:35:35 UTC
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
Comment 1 Marc Glisse 2012-10-11 21:39:01 UTC
#include <iostream>

struct A
{
    A(std::ostream& s);
};

int main() {
    A(std::cout);
}
Comment 2 Andrew Pinski 2012-10-11 21:41:13 UTC
Looks like GCC is getting confused if the constructor is going to be a function declaration or not.
Comment 3 Andrew Pinski 2012-10-11 21:45:28 UTC
(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);
}
Comment 4 Andrew Pinski 2012-10-11 21:47:24 UTC
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.
Comment 5 Andrew Pinski 2012-10-11 21:52:06 UTC
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.
Comment 6 Andrew Pinski 2012-10-11 21:54:39 UTC
Dup of bug 29834

*** This bug has been marked as a duplicate of bug 29834 ***