Topic: Who said in C++ there are no functions with different result types and same signatures?
Author: Oleg Zabluda <zabluda@math.psu.edu>
Date: 1998/01/07 Raw View
Alexey N. Solofnenko <trelony@typhoon.spb.ru> wrote:
: Don't say I am wrong, there are no such functions. I know it (and don't like it). But there
: is one exception - casting operators. It can help us to write something which will look like
: several functions with different result types.
: class foo {
: public:
: foo (_arglist) : arglist(_alglist) {}
: operator T1() { do_smth1;}
: operator T2() { do_smth2;}
: ............
: private:
: arglist;
: };
: And now we can write :
: T1 t1=foo(...);
: T2 t2=foo(...); // different calls !!!!
Cool.
I often use the following:
template<class T> get(istream& os)
{
T tmp;
cin >> tmp;
return tmp;
}
void f()
{
switch ( const int x = get<int>(cin) ) {}
}
This way x can be defined in strange places and x can be constant.
There is one problem, however. I have to repeat "int" twice.
Your idea allows me to improve the idiom:
class get{
private:
istream& is_;
public:
get(istream& is) : is_(is) {}
template<class T> operator T() const
{
T tmp;
is >> tmp;
return tmp;
}
};
void f()
{
switch ( const int i = get(cin) ) {}
}
Egcs does not find an appropriate conversion operator, though.
Is what I wrote legal?
Oleg.
--
Life is a sexually transmitted, 100% lethal disease.
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: "Alexey N. Solofnenko" <trelony@typhoon.spb.ru>
Date: 1998/01/08 Raw View
This is a multi-part message in MIME format.
--------------279A7784713491A5CB564905
Content-Type: text/plain; charset=koi8-r
Content-Transfer-Encoding: 7bit
Hi, Dietmar!
I always thought that it is prohibited to write template parameterized with return type. I
have CD2 on my hard drive but I did not really read it (I will). But in your example you wrote
result type explicitly and I tried to avoid it. That is the matter. C creators trusted
programmers (maybe too much). C++ committee does not. I want to be able to say to compiler "Yes,
these functions differ only with result types and that is what I want.", but I can not. The
programmer knows what he is doing.
Best wishes,
Alexey.
Dietmar Kuehl wrote:
> Hi,
> Alexey N. Solofnenko (trelony@typhoon.spb.ru) wrote:
> : Don't say I am wrong, there are no such functions. I know it
> : (and don't like it). But there is one exception - casting operators.
> : It can help us to write something which will look like several
> : functions with different result types.
>
> [example snipped]
>
> Actually, there is another method which produces something similar:
> explicit template argument for member template functions:
>
> class foo
> {
> public:
> ...
> template <typename T> T bar();
> template <> T1 bar<T1>() { do_something1; }
> template <> T2 bar<T2>() { do_something2; }
> };
>
> At least I think it should work: the later two declarations should be
> specializations of the first one. However, I have no compiler which
> compiles this code... But egcs compiles at least this one:
>
> #include <iostream.h>
>
> template <typename T> struct foobar;
> template <> struct foobar<long> { static long bar() { return 17; } };
> template <> struct foobar<int> { static int bar() { return 13; } };
>
> template <typename T> T bar() { return foobar<T>::bar(); }
>
> int main()
> {
> cout << bar<int>() << endl;
> cout << bar<long>() << endl;
> return 0;
> }
>
> ... and produces the expected result :-)
> --
> <mailto:dietmar.kuehl@claas-solutions.de>
> <http://www.informatik.uni-konstanz.de/~kuehl/>
> I am a realistic optimist - that's why I appear to be slightly pessimistic
> ---
> [ comp.std.c++ is moderated. To submit articles: try just posting with ]
> [ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
> [ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
> [ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
> [ Comments? mailto:std-c++-request@ncar.ucar.edu ]
--------------279A7784713491A5CB564905
Content-Type: text/x-vcard; charset=koi8-r; name="vcard.vcf"
Content-Transfer-Encoding: 7bit
Content-Description: Card for Alexey N. Solofnenko
Content-Disposition: attachment; filename="vcard.vcf"
begin: vcard
fn: Alexey N. Solofnenko
n: Solofnenko;Alexey N.
org: Typhoon Software, Inc.
adr: ;;;St. Petersburg;;;Russia
email;internet: trelony@typhoon.spb.ru
title: programmer
x-mozilla-cpt: ;0
x-mozilla-html: TRUE
version: 2.1
end: vcard
--------------279A7784713491A5CB564905--
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: "Alexey N. Solofnenko" <trelony@typhoon.spb.ru>
Date: 1998/01/05 Raw View
This is a multi-part message in MIME format.
--------------782D76C4D963E03E5E07AB23
Content-Type: text/plain; charset=koi8-r
Content-Transfer-Encoding: 7bit
Hi!
Don't say I am wrong, there are no such functions. I know it (and don't like it). But there
is one exception - casting operators. It can help us to write something which will look like
several functions with different result types.
class foo {
public:
foo (_arglist) : arglist(_alglist) {}
operator T1() { do_smth1;}
operator T2() { do_smth2;}
............
private:
arglist;
};
And now we can write :
T1 t1=foo(...);
T2 t2=foo(...); // different calls !!!!
As in case with static methods and since it can be emulated maybe it is good idea to include
the
possibility of defining such functions in the standard?
Best wishes,
Alexey.
--------------782D76C4D963E03E5E07AB23
Content-Type: text/x-vcard; charset=koi8-r; name="vcard.vcf"
Content-Transfer-Encoding: 7bit
Content-Description: Card for Alexey N. Solofnenko
Content-Disposition: attachment; filename="vcard.vcf"
begin: vcard
fn: Alexey N. Solofnenko
n: Solofnenko;Alexey N.
org: Typhoon Software, Inc.
adr: ;;;St. Petersburg;;;Russia
email;internet: trelony@typhoon.spb.ru
title: programmer
x-mozilla-cpt: ;0
x-mozilla-html: TRUE
version: 2.1
end: vcard
--------------782D76C4D963E03E5E07AB23--
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: kuehl@horn.informatik.uni-konstanz.de (Dietmar Kuehl)
Date: 1998/01/06 Raw View
Hi,
Alexey N. Solofnenko (trelony@typhoon.spb.ru) wrote:
: Don't say I am wrong, there are no such functions. I know it
: (and don't like it). But there is one exception - casting operators.
: It can help us to write something which will look like several
: functions with different result types.
[example snipped]
Actually, there is another method which produces something similar:
explicit template argument for member template functions:
class foo
{
public:
...
template <typename T> T bar();
template <> T1 bar<T1>() { do_something1; }
template <> T2 bar<T2>() { do_something2; }
};
At least I think it should work: the later two declarations should be
specializations of the first one. However, I have no compiler which
compiles this code... But egcs compiles at least this one:
#include <iostream.h>
template <typename T> struct foobar;
template <> struct foobar<long> { static long bar() { return 17; } };
template <> struct foobar<int> { static int bar() { return 13; } };
template <typename T> T bar() { return foobar<T>::bar(); }
int main()
{
cout << bar<int>() << endl;
cout << bar<long>() << endl;
return 0;
}
... and produces the expected result :-)
--
<mailto:dietmar.kuehl@claas-solutions.de>
<http://www.informatik.uni-konstanz.de/~kuehl/>
I am a realistic optimist - that's why I appear to be slightly pessimistic
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]