D
dave_dp
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.
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.