Topic: restrictions on compiler optimizations?
Author: dhruvbird@gmx.net (Dhruv)
Date: Mon, 9 Jun 2003 01:37:24 +0000 (UTC) Raw View
tslettebo@chello.no.nospam ("Terje Sletteb ") wrote in message
[snip]......
> The latter way may not always work that well, if you have overloaded
> function templates. There's a core issue for this
> (http://anubis.dkuug.dk/jtc1/sc22/wg21/docs/cwg_closed.html#200).
>
>
> Regards,
>
> Terje
>
Thanks,
-Dhruv.
> ---
> [ 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 ]
> [ --- Please see the FAQ before posting. --- ]
> [ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
---
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: tslettebo@chello.no.nospam ("Terje Sletteb ")
Date: Mon, 26 May 2003 00:45:38 +0000 (UTC) Raw View
"Dhruv" <dhruvbird@gmx.net> wrote in message
news:cf18e89.0305250246.1c488314@posting.google.com...
> I wished that compilers performed such optimizations:
> Does the standard stop them form??????
>
> void foo (const some_class&) //look ma, no variable name.
> { //do something }
>
> Now, when foo is called as such:
> foo (some_class()); //default ctor for some_class.
>
> The constructor is always invoked. I want the ctor not to be invoked
> for such cases??? Any suggestions??????
>
> I want to do this primarily for detecting the type in a template
> function. Like a generic destroy function. The argument is actually
> never used, but its type is used......
You may use a technique that is present in Loki and Boost's MPL, called
"Type2Type" and "identity", respectively. It works by wrapping any type in a
lightweight object (no data members and no constructor), which may well be
optimised away. It looks like this:
template<class T>
struct identity
{
typedef T type;
};
> template <class t> void destroy (pointer p, const_reference)
> {
> call destructor for p.
> p->~t();
> }
>
> Here, the 2nd parameter is never used, only its type is used. I've be
> using cheap hacks like passing a caster pointer, etc... But it makes
> my hair stand up.............
Passing a pointer is one way, using Type2Type/identity is another. It may be
used like this:
template<class T>
void f(identity<T>) // Parameter not used
{
}
f(identity<some_type>()); // Call f()
It may be used as a kind of way to simulate partial specialisation of
function templates.
Regards,
Terje
---
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: qrczak@knm.org.pl ("Marcin 'Qrczak' Kowalczyk")
Date: Tue, 27 May 2003 17:16:25 +0000 (UTC) Raw View
Dhruv wrote:
> The argument is actually never used, but its type is used......
You can have type parameters which aren't used inside value parameters,
and explicitly provide them when using the template:
template<class t> void do_something() {...}
do_something<int>();
--
__("< Marcin Kowalczyk
\__/ qrczak@knm.org.pl
^^ http://qrnik.knm.org.pl/~qrczak/
---
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: dhruvbird@gmx.net (Dhruv)
Date: Tue, 27 May 2003 17:25:37 +0000 (UTC) Raw View
tslettebo@chello.no.nospam ("Terje Sletteb ") wrote in message news:<Ef8Aa.8589$KF1.135045@amstwist00>...
> "Dhruv" <dhruvbird@gmx.net> wrote in message
> news:cf18e89.0305250246.1c488314@posting.google.com...
> Passing a pointer is one way, using Type2Type/identity is another. It may be
> used like this:
>
> template<class T>
> void f(identity<T>) // Parameter not used
> {
> }
>
> f(identity<some_type>()); // Call f()
>
> It may be used as a kind of way to simulate partial specialisation of
> function templates.
>
>
> Regards,
>
> Terje
Thanks for that!!! :-) Nice trick.....
Well, I want to know if there is a way to find out if the ctor for an
object is actually called or not? I tried putting a cout<< in the
ctor, but figured that that would make the compiler always call it.
I could peek into the asm output, but I've just shifted from Windows
to GNU/Linux, and the AT&T syntax is kind of headache prone for me (at
least upto now). Also, g++ (gcc) doesn't even comment code when the -S
option is specified, so I really cannot make out what code is for what
code. I know that can be hard for optimized code, but if it's
commented, it could help....
-Dhruv.
---
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: tslettebo@chello.no.nospam ("Terje Sletteb ")
Date: Tue, 27 May 2003 18:26:33 +0000 (UTC) Raw View
"Dhruv" <dhruvbird@gmx.net> wrote in message
news:cf18e89.0305270349.40a45dd1@posting.google.com...
> tslettebo@chello.no.nospam ("Terje Sletteb ") wrote in message
news:<Ef8Aa.8589$KF1.135045@amstwist00>...
> > "Dhruv" <dhruvbird@gmx.net> wrote in message
> > news:cf18e89.0305250246.1c488314@posting.google.com...
> > Passing a pointer is one way, using Type2Type/identity is another. It
may be
> > used like this:
> >
> > template<class T>
> > void f(identity<T>) // Parameter not used
> > {
> > }
> >
> > f(identity<some_type>()); // Call f()
> >
> > It may be used as a kind of way to simulate partial specialisation of
> > function templates.
>
> Thanks for that!!! :-) Nice trick.....
>
> Well, I want to know if there is a way to find out if the ctor for an
> object is actually called or not? I tried putting a cout<< in the
> ctor, but figured that that would make the compiler always call it.
In all likelihood, the constructor won't be called, as it's
default-provided and trivial, since identity<> don't even have any data
members.
It might even optimise away the parameter passing, at least if the
function is inlined.
Also, as Marcin Kowalczyk says in another posting, another way is to
explicitly provide the template parameter at the call site, if possible.
The latter way may not always work that well, if you have overloaded
function templates. There's a core issue for this
(http://anubis.dkuug.dk/jtc1/sc22/wg21/docs/cwg_closed.html#200).
Regards,
Terje
---
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: dhruvbird@gmx.net (Dhruv)
Date: Sun, 25 May 2003 14:20:16 +0000 (UTC) Raw View
I wished that compilers performed such optimizations:
Does the standard stop them form??????
void foo (const some_class&) //look ma, no variable name.
{ //do something }
Now, when foo is called as such:
foo (some_class()); //default ctor for some_class.
The constructor is always invoked. I want the ctor not to be invoked
for such cases??? Any suggestions??????
I want to do this primarily for detecting the type in a template
function. Like a generic destroy function. The argument is actually
never used, but its type is used......
template <class t> void destroy (pointer p, const_reference)
{
call destructor for p.
p->~t();
}
Here, the 2nd parameter is never used, only its type is used. I've be
using cheap hacks like passing a caster pointer, etc... But it makes
my hair stand up.............
---
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]