* struct->struct->class in C++ no constructor
@ 2008-07-21 21:14 Wesley Smith
2008-07-21 21:19 ` Chris Jefferson
` (3 more replies)
0 siblings, 4 replies; 6+ messages in thread
From: Wesley Smith @ 2008-07-21 21:14 UTC (permalink / raw)
To: GCC-help
Hi,
I've encountered a problem in GCC 4.2.3 that didn't exist in GCC
4.0.x. Let's say I have:
template<class T>
Tube{
public:
Tube();
~Tube();
};
struct Graph {
Tube<Ugen> t;
}
struct Clock {
Graph g;
}
At issue is that Tube<T>'s constructor isn't getting called when Clock
is allocated on the stack. Is this compliant with the C++ standard?
I thought all classes had their constructors called when instantiated
as an object even as members of structs. Any ideas?
thanks,
wes
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: struct->struct->class in C++ no constructor
2008-07-21 21:14 struct->struct->class in C++ no constructor Wesley Smith
@ 2008-07-21 21:19 ` Chris Jefferson
2008-07-21 21:23 ` John Fine
` (2 subsequent siblings)
3 siblings, 0 replies; 6+ messages in thread
From: Chris Jefferson @ 2008-07-21 21:19 UTC (permalink / raw)
To: Wesley Smith; +Cc: GCC-help
2008/7/21 Wesley Smith <wesley.hoke@gmail.com>:
> Hi,
> I've encountered a problem in GCC 4.2.3 that didn't exist in GCC
> 4.0.x. Let's say I have:
>
> template<class T>
> Tube{
> public:
> Tube();
> ~Tube();
> };
>
> struct Graph {
> Tube<Ugen> t;
> }
>
> struct Clock {
> Graph g;
> }
>
> At issue is that Tube<T>'s constructor isn't getting called when Clock
> is allocated on the stack. Is this compliant with the C++ standard?
> I thought all classes had their constructors called when instantiated
> as an object even as members of structs. Any ideas?
Can you give a complete program which shows your problem?
Chris
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: struct->struct->class in C++ no constructor
2008-07-21 21:14 struct->struct->class in C++ no constructor Wesley Smith
2008-07-21 21:19 ` Chris Jefferson
@ 2008-07-21 21:23 ` John Fine
2008-07-22 13:20 ` Wesley Smith
2008-07-23 10:58 ` Alexandre Oliva
2008-07-23 11:07 ` Eljay Love-Jensen
3 siblings, 1 reply; 6+ messages in thread
From: John Fine @ 2008-07-21 21:23 UTC (permalink / raw)
To: Wesley Smith; +Cc: GCC-help
Can you provide a complete example?
As you described it, the constructor should be called. But you haven't
given enough information for anyone to duplicate the situation in which
the constructor isn't called.
Was the definition of the constructor provided in the same compilation
or was it external? If provided, how do you know it wasn't called
(possibly inlined and optimized to the point that you wouldn't know)?
Wesley Smith wrote:
>
> At issue is that Tube<T>'s constructor isn't getting called when Clock
> is allocated on the stack. Is this compliant with the C++ standard?
> I thought all classes had their constructors called when instantiated
> as an object even as members of structs. Any ideas?
>
>
>
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: struct->struct->class in C++ no constructor
2008-07-21 21:23 ` John Fine
@ 2008-07-22 13:20 ` Wesley Smith
0 siblings, 0 replies; 6+ messages in thread
From: Wesley Smith @ 2008-07-22 13:20 UTC (permalink / raw)
To: John Fine; +Cc: GCC-help
Hi,
I had a breakpoint in the constructor, so I know it wasn't called.
Also had some print statements in there. It's a non trivial function,
so it shouldn't be optimized out and I'm also building debug. I'll
try and extract it from the project and post a concrete example.
thanks,
wes
On Mon, Jul 21, 2008 at 2:18 PM, John Fine <johnsfine@verizon.net> wrote:
> Can you provide a complete example?
>
> As you described it, the constructor should be called. But you haven't
> given enough information for anyone to duplicate the situation in which the
> constructor isn't called.
>
> Was the definition of the constructor provided in the same compilation or
> was it external? If provided, how do you know it wasn't called (possibly
> inlined and optimized to the point that you wouldn't know)?
>
> Wesley Smith wrote:
>>
>> At issue is that Tube<T>'s constructor isn't getting called when Clock
>> is allocated on the stack. Is this compliant with the C++ standard?
>> I thought all classes had their constructors called when instantiated
>> as an object even as members of structs. Any ideas?
>>
>>
>>
>
>
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: struct->struct->class in C++ no constructor
2008-07-21 21:14 struct->struct->class in C++ no constructor Wesley Smith
2008-07-21 21:19 ` Chris Jefferson
2008-07-21 21:23 ` John Fine
@ 2008-07-23 10:58 ` Alexandre Oliva
2008-07-23 11:07 ` Eljay Love-Jensen
3 siblings, 0 replies; 6+ messages in thread
From: Alexandre Oliva @ 2008-07-23 10:58 UTC (permalink / raw)
To: Wesley Smith; +Cc: GCC-help
On Jul 21, 2008, "Wesley Smith" <wesley.hoke@gmail.com> wrote:
> At issue is that Tube<T>'s constructor isn't getting called when Clock
> is allocated on the stack.
Two thoughts:
- copy constructor being called instead
- syntax that declares a function returning a Clock, rather than an
object of type Clock
Without the definition, it's hard to give any definitive answer.
--
Alexandre Oliva http://www.lsd.ic.unicamp.br/~oliva/
Free Software Evangelist oliva@{lsd.ic.unicamp.br, gnu.org}
FSFLA Board Member ¡Sé Libre! => http://www.fsfla.org/
Red Hat Compiler Engineer aoliva@{redhat.com, gcc.gnu.org}
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: struct->struct->class in C++ no constructor
2008-07-21 21:14 struct->struct->class in C++ no constructor Wesley Smith
` (2 preceding siblings ...)
2008-07-23 10:58 ` Alexandre Oliva
@ 2008-07-23 11:07 ` Eljay Love-Jensen
3 siblings, 0 replies; 6+ messages in thread
From: Eljay Love-Jensen @ 2008-07-23 11:07 UTC (permalink / raw)
To: Wesley Smith, GCC-help
Hi Wes,
Worked on my machine. [See code below.]
Can you provide a compile-able example which illustrates the problem?
The example you provided was too abbreviated, and did not compile.
Thanks,
--Eljay
- - - -
#include <iostream>
template <typename T>
class Tube
{
public:
Tube();
~Tube();
};
template <typename T>
Tube<T>::Tube()
{
std::cerr << "Tube ctor\n";
}
template <typename T>
Tube<T>::~Tube()
{
std::cerr << "Tube dtor\n";
}
typedef int Ugen;
struct Graph
{
Tube<Ugen> t;
};
struct Clock
{
Graph g;
};
void TestOnStack()
{
Clock clock;
}
int main()
{
TestOnStack();
}
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2008-07-23 10:58 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-07-21 21:14 struct->struct->class in C++ no constructor Wesley Smith
2008-07-21 21:19 ` Chris Jefferson
2008-07-21 21:23 ` John Fine
2008-07-22 13:20 ` Wesley Smith
2008-07-23 10:58 ` Alexandre Oliva
2008-07-23 11:07 ` Eljay Love-Jensen
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).