|  | Can't template operator << be private? |  | |
| | | lv |  |
| Posted: Thu Sep 04, 2008 4:24 am Post subject: Can't template operator << be private? |  |
Hi all!
I have a class with a private template operator << like this:
class MyTest { template <typename T> MyTest & operator << (T const & t) { std::cout << t << std::endl; return *this; } };
But the operator is accessible outside the class. However, if I make it a non-template operator that only accepts an integer, it's truly private. The code was tested on vc9.0.
-- [ See LINK for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
| |
| | | Jiang |  |
| Posted: Thu Sep 04, 2008 9:28 am Post subject: Re: Can't template operator << be private? |  |
On Sep 4, 1:24 pm, lv <lv.jc...@gmail.com> wrote:
| Quote: | Hi all!
I have a class with a private template operator << like this:
class MyTest { template <typename T MyTest & operator << (T const & t) { std::cout << t << std::endl; return *this; }
};
But the operator is accessible outside the class. However, if I make it a non-template operator that only accepts an integer, it's truly private.
|
Access specifiers should be respected for both template and non-template code.
| Quote: | The code was tested on vc9.0.
|
Test more please. Seems vc9 is broken here.
* icl 10.1 * GCC (4.3.2/4.3.1/4.2.2/3.4.4) * como 4.3.10.1
reject your access code in my mind.
Regards,
Jiang
-- [ See LINK for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
| |
| | | Thomas Richter |  |
| Posted: Thu Sep 04, 2008 9:28 am Post subject: Re: Can't template operator << be private? |  |
lv schrieb:
| Quote: | Hi all!
I have a class with a private template operator << like this:
class MyTest { template <typename T MyTest & operator << (T const & t) { std::cout << t << std::endl; return *this; } };
But the operator is accessible outside the class.
|
For which definition of "accessible"? Could you please post a complete example that demonstrates the problem?
So long, Thomas
-- [ See LINK for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
| |
| | | lv |  |
| Posted: Thu Sep 04, 2008 7:31 pm Post subject: Re: Can't template operator << be private? |  |
On Sep 4, 5:28 pm, Thomas Richter <t...@math.tu-berlin.de> wrote:
| Quote: | lv schrieb:
For which definition of "accessible"? Could you please post a complete example that demonstrates the problem?
So long, Thomas
|
Well, I should have made it clearer. I meant the following code compiles ,
#include <iostream> class MyTest { template <typename T> MyTest & operator << (T const & t) { std::cout << t << std::endl; return *this; } };
int main() { MyTest test; test << 10;
return 0; }
while this one doesn't compile:
#include <iostream> class MyTest { MyTest & operator << (int const & t) { std::cout << t << std::endl; return *this; } };
int main() { MyTest test; test << 10;
return 0; }
I tested it again with vc8.0 and got the same result.
-- [ See LINK for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
| |
| | | Guest |  |
| Posted: Thu Sep 04, 2008 10:36 pm Post subject: Re: Can't template operator << be private? |  |
On Sep 4, 12:31 pm, lv <lv.jc...@gmail.com> wrote:
| Quote: | On Sep 4, 5:28 pm, Thomas Richter <t...@math.tu-berlin.de> wrote: For which definition of "accessible"? Could you please post a complete example that demonstrates the problem? Well, I should have made it clearer. I meant the following code compiles , [snip] |
Try your code on LINK You will see that both samples do not compile. It's a bug in visual studios apparently.
-- [ See LINK for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
| |
|
|