-3

I try to force my compiler to replace any std::cout occurrence in my code with something. But when I write something like that:

#define std::cout (*some code*)

My compiler spit on my face. Is there a way to do this ?

EDIT :

Here is the code snippet:

# define std::cout (std_out << std::cout)

(std_out is a file I've previously open)

and the error on a line with a std::out occurence

the global scope has no "cout"

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
PanzerKadaver
  • 363
  • 3
  • 12
  • 1
    To do what? You haven't told us what it is you tried to replace it with or what error you got... – Borgleader Jan 20 '14 at 11:31
  • 1
    Maybe a search-replace would be a better idea here. Replace every occurrence of `std::cout` with something else that can be easily modified to call `std::cout` when you need to revert the changes. – Vittorio Romeo Jan 20 '14 at 11:31
  • Also tell us what it spat? – legends2k Jan 20 '14 at 11:32
  • I suspect colon symbol can't be used as identifier. – loentar Jan 20 '14 at 11:32
  • 4
    To redirect things, better use `std::cout.rdbuf(...)`. – Marc Glisse Jan 20 '14 at 11:33
  • 1
    @MarcGlisse To redirect, you just don't use `std::cout`. Except in small experiments, you really shouldn't have an `std::cout <<` in your code to begin with. – James Kanze Jan 20 '14 at 11:38
  • 1
    In case of doubt why a macro doesn't work, try calling your compiler with the `-E` flag. It would tell you that with your `#define std::cout (std_out << std::cout)` from the question `std::cout << 42;` becomes `::cout (std_out << std::cout)::cout << 42;` This isn't what you wanted, is it? – Lmis Jan 20 '14 at 11:38

3 Answers3

6

You define an identifier, not an arbitrary expression. std is an identifier; your define will cause the compiler to replace every instance of the identifier std with ::cout (*some code*). So it's not surprising that the compiler doesn't like it: std::cout << toto becomes ::cout (*some code*)::cout << toto and std::vector becomes ::cout (*some code*)::vector.

If you'd explain what you're actually trying to achieve, we could probably help you better.

James Kanze
  • 150,581
  • 18
  • 184
  • 329
1

I try to force my compiler to replace any std::cout occurence in my code with something

That's a bad idea. If you are looking for configurable behavior on your output stream, replace all occurrences of std::cout in your code with out, and declare out as std::ostream& out (= whatever stream type you may need).

My compiler spit on my face. Is there a way to do this ?

Not as such. No. You could write:

#define OUTPUT std::cout

OUTPUT << "a = " << a << std::endl;

but you needing a #define to disable (or redirect) your output stream is a sign of bad design (i.e. your define is not the problem you should be trying to solve).

utnapistim
  • 26,809
  • 3
  • 46
  • 82
0

You can make your own version of cout, that actually calls cout, you can place any custom code there:

std::ostream& my_cout() {
   /// ...
   return std::cout << "a custom message";
}

int main() {
   my_cout() << " hi" << std::endl;
}