473,405 Members | 2,262 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Incomplete types and templates

Hi folks,

I'm interested as to what extent using incomplete types doesn't result
in a undefined behavior, more generally it touches the usage of
incomplete types.. for example, it is stated that you can't use
incomplete types to create objects of type T(that is incomplete), you
can't use pointer arithmetic on T* types, you can't use new T..,...,
you can't use sizeof... but you are allowed to have pointers and
references to T... This is all good so far, but consider this example:

template <typename T>
class BaseClass {
public:
size_t sizeOfArgument() { return sizeof(T); }
};

class Dummy : public BaseClass<Dummy{
};
int main() {
Dummy dummy;
size_t dummySize = dummy.sizeOfArgument();
}

Now when deriving from BaseClass we pass incomplete type(Dummy) as a
template argument, and in main we call member method that uses sizeof
on incomplete type, does this code result in a undefined behavior? and
is that any different from:

template <typename T>
class Dummy : public BaseClass<Dummy<T {
};

int main() {
Dummy dummy<char>;
size_t dummySize = dummy.sizeOfArgument();
}

If it is legal then why? The only argument I can think of right now is
that in main when we instantiate sizeOfArgument member method(member
function isn't instantiated unless it's used) Dummy is complete type...
I'm lost anyways..
TIA.

--
Regards,
David

Dec 23 '06 #1
2 1996

da*****@mail.ru wrote:
If it is legal then why? The only argument I can think of right now is
that in main when we instantiate sizeOfArgument member method(member
function isn't instantiated unless it's used) Dummy is complete type...
I'm lost anyways..
I can't answer your questions exactly but look up CRTP which stands for
Curiously Recurring Template Pattern. Its all to do with the fact that
templates are instantiated only when required AFAIK.

Also you can do this with CRTP as an alternative to virtual functions
--->

Not sure if it helps but its quite cool anyway!

regards
Andy Little

-----------------

#include <iostream>

template <typename T>
class BaseClass {
public:
size_t sizeOfArgument() { return sizeof(T); }

void f()
{
T * t = static_cast<T*>(this);
t->derived_f();
}
};
class Dummy : public BaseClass<Dummy{
public:
void derived_f()
{
std::cout << "Hello from Dummy\n";
}
};

class Wummy : public BaseClass<Wummy{
public:
void derived_f()
{
std::cout << "Hello from Wummy\n";
}
};

int main() {

Dummy dummy;
size_t dummySize = dummy.sizeOfArgument();
dummy.f();
Wummy wummy;
wummy.f();
}

/*
output:

Hello from Dummy
Hello from Wummy

*/

Dec 23 '06 #2

kwikius wrote:
da*****@mail.ru wrote:
If it is legal then why? The only argument I can think of right now is
that in main when we instantiate sizeOfArgument member method(member
function isn't instantiated unless it's used) Dummy is complete type...
I'm lost anyways..

I can't answer your questions exactly but look up CRTP which stands for
Curiously Recurring Template Pattern. Its all to do with the fact that
templates are instantiated only when required AFAIK.

Also you can do this with CRTP as an alternative to virtual functions
--->

Not sure if it helps but its quite cool anyway!

regards
Andy Little

-----------------

#include <iostream>

template <typename T>
class BaseClass {
public:
size_t sizeOfArgument() { return sizeof(T); }

void f()
{
T * t = static_cast<T*>(this);
t->derived_f();
}
};
class Dummy : public BaseClass<Dummy{
public:
void derived_f()
{
std::cout << "Hello from Dummy\n";
}
};

class Wummy : public BaseClass<Wummy{
public:
void derived_f()
{
std::cout << "Hello from Wummy\n";
}
};

int main() {

Dummy dummy;
size_t dummySize = dummy.sizeOfArgument();
dummy.f();
Wummy wummy;
wummy.f();
}

/*
output:

Hello from Dummy
Hello from Wummy

*/
Hi,

Yes, I happen to read-up on CRTP but I didn't mention its name because
I'm concerned generally about incomplete types and their usage, that
doesn't only include CRTP(hence my example) but I can also reproduce
same type of thing without touching CRTP. for example:

template <typename T>
class BaseClass {
public:
size_t sizeOfArgument() { return sizeof(T); }

};

class SomeClass;
BaseClass<SomeClasssomeGlobal;

class SomeClass { };

int main() {
size_t sizeOfSmth = someGlobal.sizeOfArgument();

}

So to get back to my original question, am I right in assumption that
at the point of instantiation of template member method sizeOfArgument,
type is complete that's why it isn't UB? So if I'll make SomeClass
definition out of main's reach it would be therefore an UB?

TIA.

--
David

Dec 23 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Andrew Ward | last post by:
Hi All, Considering the following code: struct A; struct B { std::list<A> l; };
4
by: Mikhail N. Kupchik | last post by:
Hi All. I have a question regarding C++ programming language standard. It is related to standard library, not to the core language. Is it portable to instantiate template class std::list<>...
1
by: andrew | last post by:
Hi, I'm a C++ newbie, so apologies for this rather basic question. I've searched for help and, while I understand the problem (that the outer class is not yet defined), I don't understand what...
0
by: Steven T. Hatton | last post by:
I copied what I believe is a complete representation of <iosfwd> from ISO/IEC 14882:2003(E). My understanding of the rules covering typedefs, templates and forward declarations is not very solid. ...
5
by: Paul F. Dietz | last post by:
Is the following legal C? struct foo; struct foo (*p); /* Pointer to array of 10 foo structures */ struct foo { int bar; int baz; }; main() { printf("%d\n", sizeof(*p)); } Paul Dietz...
6
by: Eric Smith | last post by:
Is a structure containing an incomplete array as its last element (per paragraph 2 of section 6.7.2.1 of ISO/IEC 9899:1999 (E)) itself an incomplete type? That appears to be indicated by paragraph...
7
by: Michael Birkmose | last post by:
Hi everyone!, Are pointers to incomplete types allowed in ANSI C? I can see that pointer arithmic on pointers to incomple types is impossible, however there are situations where it can be...
2
by: Kai-Uwe Bux | last post by:
Does the following have undefined behavior? struct X; struct Y { X * x_ptr; Y ( void ) : x_ptr ( 0 )
50
by: Juha Nieminen | last post by:
I asked a long time ago in this group how to make a smart pointer which works with incomplete types. I got this answer (only relevant parts included): ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.