Re: Isn't function resolution consistent?
DeMarcus <use_my_alias_here@hotmail.com> writes:
Paul Bibbings wrote:
DeMarcus <use_my_alias_here@hotmail.com> writes:
I solved it! I think. How about this in A.hpp?
template<typename T>
inline A&& operator<<( A&& a, const T& t )
{
std::cout << "Delegating..." << std::endl;
return operator<<( a, t );
}
Clearly you are avoiding infinite recursion here in the call to return
operator<<( a, t ). How are you achieving that? What /other/
definition of op<< are you using? The one defined in the class A? But
that returns A&, doesn't it? And then that shouldn't bind to the return
of A&& here.
I don't know how I achieve that. For that example I stripped
everything so I only have this one left (except for the delegator).
A& operator<<( A& a, const B& b )
{
std::cout << "Non-member Function" << std::endl;
return a;
}
Try this simple example that "worked" for me.
How can the following work, since you are using std::ostream without
including the appropriate header in B.hpp?
//B.hpp (with appropriate #ifndef B_HPP_)
class B
{
};
std::ostream& operator<<( std::ostream& s, const B& b );
// B.cpp
std::ostream& operator<<( std::ostream& s, const B& b )
{
return s << "B";
}
How can the following work, since you do not include the appropriate
header to make std::cout, std::endl, etc., available?
// main.cpp
#include "B.hpp"
#include <sstream>
void fnc( const std::ostream& s )
{
std::cout << s.rdbuf() << std::endl;
}
template<typename T>
inline std::ostream&& operator<<( std::ostream&& s, const T& t )
{
return operator<<( s, t );
}
int main()
{
B b;
fnc( std::stringstream() << b );
}
There are other headers missing too. (This is what I was trying to
convey in another post. It is _crucial_ that you post the *exact* code
that you have compiled, or else how can I be sure that my `guesses' at
what is missing don't introduce further problems masking the ones you
are experiencing? If your code needs "appropriate #ifndef" include
guards, put them in!)
However...
As I've mentioned in another post, gcc-4.4.1 is an outdated
implementation with regards to rvalue references and how these are
handled, effectively rendering it worthless (and, indeed, misleading) in
this regard. To illustrate, whilst I can get the code (suitably
corrected) to compile and produce the `expected' results in gcc-4.4.1,
it is nevertheless incorrect according to the specification as it now
stands.
18:54:55 Paul Bibbings@JIJOU
/cygdrive/d/CPPProjects/CLCPP/consistent_resolution/partII
$i686-pc-cygwin-g++-4.5.0 -std=c++0x -o B main.cpp B.cpp
main.cpp: In function ??std::ostream&& operator<<(std::ostream&&,
const T&) [with T = B, std::ostream = std::basic_ostream<char>]??:
main.cpp:21:32: instantiated from here
main.cpp:15:28: error: invalid initialization of reference of type
??std::ostream&&?? from expression of type ??std::ostream??
In short, experimenting with rvalue references using gcc-4.4.1 is simply
a no-starter, unfortunately. Effectively you are writing code to follow
an implementation that was never ratified as standard, and nor will it
be, rendering it a worthwhile exercise only on historical grounds, and
nothing else.
HTH
Regards
Paul Bibbings