I am working with a large C++ program which, for reasons of backward
compatibility, uses C's printf and fprintf rather than iostreams.
For a certain type of build I want to provide new functions that
change the behaviour of printf and fprintf, and I planned to make them
available using a convenient namespace, called 'herring', say, like this:
using herring::printf ;
using herring::fprint f;
Now, all the other classes and functions etc are in their own
namespace, called 'haddock' say. So all the code has the lines
namespace haddock{
...
}
around it. It would be most convenient to put the 'using' directives
just inside the 'namespace', like this:
#include <stdio.h>
namespace herring{
int printf(const char*, ...);
int fprintf(FILE*, const char*, ...);
}
namespace haddock{
using herring::fprint f;
using herring::printf ;
void halibut(){
FILE *fp;
fprintf(fp, "hoho");
printf("haha");
}
}
But this does not work: g++ -c gives me
name.C: In function `void haddock::halibu t()':
name.C:15: call of overloaded `fprintf(FILE*& , const char[5])' is
ambiguous
name.C:5: candidates are: int herring::fprint f(FILE*, const char*, ...)
/usr/local/lib/gcc-lib/i686-pc-linux-gnu/3.2.1/include/stdio.h:307:
int fprintf(FILE*, const char*, ...)
on a bog-standard RH Linux box with g++ 3.2.1
However, this does work:
#include <stdio.h>
namespace herring{
int printf(const char*, ...);
int fprintf(FILE*, const char*, ...);
}
namespace haddock{
using herring::printf ;
void halibut(){
using herring::fprint f;
FILE *fp;
fprintf(fp, "hoho");
printf("haha");
}
}
which is fine, but would mean I have to put the 'using' into every
function, rather than simply changing
namespace haddock{
to
namespace haddock{ using herring::fprint f; using herring::printf ;
(which I can actually do in a single header file).
Anyway, my question is, why does the second code compile but not the
first?
Thanks.
compatibility, uses C's printf and fprintf rather than iostreams.
For a certain type of build I want to provide new functions that
change the behaviour of printf and fprintf, and I planned to make them
available using a convenient namespace, called 'herring', say, like this:
using herring::printf ;
using herring::fprint f;
Now, all the other classes and functions etc are in their own
namespace, called 'haddock' say. So all the code has the lines
namespace haddock{
...
}
around it. It would be most convenient to put the 'using' directives
just inside the 'namespace', like this:
#include <stdio.h>
namespace herring{
int printf(const char*, ...);
int fprintf(FILE*, const char*, ...);
}
namespace haddock{
using herring::fprint f;
using herring::printf ;
void halibut(){
FILE *fp;
fprintf(fp, "hoho");
printf("haha");
}
}
But this does not work: g++ -c gives me
name.C: In function `void haddock::halibu t()':
name.C:15: call of overloaded `fprintf(FILE*& , const char[5])' is
ambiguous
name.C:5: candidates are: int herring::fprint f(FILE*, const char*, ...)
/usr/local/lib/gcc-lib/i686-pc-linux-gnu/3.2.1/include/stdio.h:307:
int fprintf(FILE*, const char*, ...)
on a bog-standard RH Linux box with g++ 3.2.1
However, this does work:
#include <stdio.h>
namespace herring{
int printf(const char*, ...);
int fprintf(FILE*, const char*, ...);
}
namespace haddock{
using herring::printf ;
void halibut(){
using herring::fprint f;
FILE *fp;
fprintf(fp, "hoho");
printf("haha");
}
}
which is fine, but would mean I have to put the 'using' into every
function, rather than simply changing
namespace haddock{
to
namespace haddock{ using herring::fprint f; using herring::printf ;
(which I can actually do in a single header file).
Anyway, my question is, why does the second code compile but not the
first?
Thanks.
Comment