Date: Thu, 22 Apr 2021 00:21:28 +0300
The following program does not compile in MS Visual Studio 19.
#include <iostream>
#include <string>
template <typename T>
class A;
template <typename T>
std::ostream &operator <<( std::ostream &, const A<T> & );
template <typename T>
class A
{
private:
T x;
public:
A( const T &x ) : x( x ) {}
friend std::ostream &::operator <<( std::ostream &, const A<T> & );
};
template <typename T>
std::ostream &operator <<( std::ostream &os, const A<T> &a )
{
return os << "a.x = " << a.x;
}
int main()
{
std::cout << A<std::string>( "Hello" ) << '\n';
}
The compiler says that operator << is not a function.
While the following program
#include <iostream>
#include <string>
template <typename T>
class A;
template <typename T>
std::ostream &f( std::ostream &, const A<T> & );
template <typename T>
class A
{
private:
T x;
public:
A( const T &x ) : x( x ) {}
friend std::ostream &::f( std::ostream &, const A<T> & );
};
template <typename T>
std::ostream &f( std::ostream &os, const A<T> &a )
{
return os << "a.x = " << a.x;
}
int main()
{
f( std::cout, A<std::string>( "Hello" ) ) << '\n';
}
compiles successfully.
What is the reason of that the first program does not compile? Is it a bug of MS Visual Studio 19 or do I have missed something from the C++ 20 Standard?
You can meet me at http://cpp.forum24.ru/ or www.stackoverflow.com or http://ru.stackoverflow.com
#include <iostream>
#include <string>
template <typename T>
class A;
template <typename T>
std::ostream &operator <<( std::ostream &, const A<T> & );
template <typename T>
class A
{
private:
T x;
public:
A( const T &x ) : x( x ) {}
friend std::ostream &::operator <<( std::ostream &, const A<T> & );
};
template <typename T>
std::ostream &operator <<( std::ostream &os, const A<T> &a )
{
return os << "a.x = " << a.x;
}
int main()
{
std::cout << A<std::string>( "Hello" ) << '\n';
}
The compiler says that operator << is not a function.
While the following program
#include <iostream>
#include <string>
template <typename T>
class A;
template <typename T>
std::ostream &f( std::ostream &, const A<T> & );
template <typename T>
class A
{
private:
T x;
public:
A( const T &x ) : x( x ) {}
friend std::ostream &::f( std::ostream &, const A<T> & );
};
template <typename T>
std::ostream &f( std::ostream &os, const A<T> &a )
{
return os << "a.x = " << a.x;
}
int main()
{
f( std::cout, A<std::string>( "Hello" ) ) << '\n';
}
compiles successfully.
What is the reason of that the first program does not compile? Is it a bug of MS Visual Studio 19 or do I have missed something from the C++ 20 Standard?
You can meet me at http://cpp.forum24.ru/ or www.stackoverflow.com or http://ru.stackoverflow.com
Received on 2021-04-21 16:21:34