the [not so] evil C++ syntax

[Edit: ] My code is indeed all a mess. Bazzy's solution further down is how to do this properly.

Hi all, this is not to ask for advice but just to complain about what a pain in the butt the c++ syntax is.

It took me like an hour of googling and trial and error to get this right:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
template<class Object> 
class List;

template<class Object>
std::fstream& operator<<(std::fstream& output, const List<Object>& theList);

template<class Object>
class List
{
friend std::fstream& operator<< <Object>(std::fstream& output, const List<Object>& theList);
public:
/*the content here*/
};

template <class Object>
std::fstream& operator<< (std::fstream& output, const List<Object>& theList)
{ output << 1; /* << ... do your output here...*/
  return output;
}


I believe not one line in the above is superfluous! What a mess...
Cheers!
Last edited on
Actually, almost all of those lines are superfluous:
1
2
3
4
5
6
7
8
9
10
template<class Object>
    class List
    {
        friend std::ostream& operator<< (std::ostream& output, const List& theList)
        {
            return output << 1;
        }
        public:
        /*the content here*/
    };
+1 to Bazzy, I just was about to post exactly the same modified code.
I don't see why you're limiting yourself to fstream, ostream is much more general.
Last edited on
You are right! I had such a hard time because I wanted the body of the function outside of the class declaration. Now it looks far more civilized!
Thanks, and cheers!
Even with the body outside the class it's not as long as in your code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
template<class Object>
    class List
    {
        template < class Obj >
            friend std::ostream& operator<< (std::ostream& output, const List<Obj>& theList);
        public:
        /*the content here*/
    };

template<class Object>
    std::ostream& operator<< (std::ostream& output, const List<Object>& theList)
    {
        return output << 1;
    }
I dont like your second solution. Does it not imply that operator << <int> will be a friend of my List<double> ? I think yes (although I am not sure).

At any rate, if my above concern is correct, it is a (-1) to the second solution (but still a +2 to your first one - I already changed the code this way).

[Edit:] @Athar: yes for the ostream. I can always change the fstream to ostream in 1 minute, so I will reserve that move for later - I prefer not to open up functionality I don't use. Just in case...
Last edited on
That is without a doubt the wrong approach.
You should not artificially bar out functionality that comes at no additional cost.
For example, using the const keyword artificially bans functionality that comes at no additional cost - such as modifying const objects (even if they live on the stack or are statically residing in the memory).

Ok, this is a far stretch of analogy on my part for the sake of arguing and shooting the breeze - but so is talking about "right" and "wrong" approach, especially in C++.
Last edited on
const correctness should be followed for a reason, so that's indeed not a good analogy.
The chance that you'll use operator<< on the "wrong" type of stream object when you actually don't mean to is essentially zero.
Last edited on
Topic archived. No new replies allowed.