473,661 Members | 2,448 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.sizeOfArg ument();
}

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.sizeOfArg ument();
}

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 2013

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.sizeOfArg ument();
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.sizeOfArg ument();
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<SomeC lasssomeGlobal;

class SomeClass { };

int main() {
size_t sizeOfSmth = someGlobal.size OfArgument();

}

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
4943
by: Andrew Ward | last post by:
Hi All, Considering the following code: struct A; struct B { std::list<A> l; };
4
2477
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<> with incomplete type? I've seen some STL implementations which allow this and some others that does not. I did not find any mentioning of this topic in the standard, maybe I searched not enough thoroughly?
1
1491
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 the "correct" solution would be. I am trying to model lists much like in Lisp, using a "Cons" object to hold two pointers - one to an Element, and the other to a further Cons. Cons is a subclass of Element, so lists can be nested.
0
1760
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. As I understand what is presented here, the typedefs are declaring typedef-names which refer to incomplete types. That is to say, they are becoming synonyms for forward declarations of class template instances. I see nothing in the Standard...
5
3726
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 dietz@dls.net
6
4193
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 22 of section 6.2.5. If so, that seems to make it difficult to allocate such structures, because sizeof() is not allowed on incomplete types (paragraph 1 of section 6.5.3.4). For instance, I've routinely done things like this: struct foo {...
7
8669
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 useful: consider this:
2
6186
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
4471
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): //------------------------------------------------------------------ template<typename Data_t> class SmartPointer { Data_t* data; void(*deleterFunc)(Data_t*);
0
8428
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8851
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8754
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8542
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7362
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4177
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4343
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2760
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1740
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.