Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

bool is<T>(V v) attempts

24 views
Skip to first unread message

Aaron Gray

unread,
Oct 21, 2022, 2:35:41 AM10/21/22
to
I have been trying to get an is() and as() functions working and as theres no dynamic is_subclass in C++ I have resulted in going back to relying on dynamic_cast !!! I cannot seem to get inbuilt ints to work though.

#include <iostream>
#include <type_traits>

template <typename T>
std::string typeof() { in
return typeid(T).name();
}

template <typename T>
std::string typeof(T t) {
return typeid(t).name();
}

template <typename T, typename V>
bool is(V* t) {
try {
return dynamic_cast<T*>(t) != nullptr;
}
catch (std::bad_cast) {
return false;
}
}

template <typename T, typename V>
bool is(V& t) {
try {
dynamic_cast<T&>(t);
return true;
}
catch (std::bad_cast) {
return false;
}
}
template <> // FAILING !
bool is<int&>(int t) {
return true;
}

template <typename T, typename V>
T& as(V t) {
return *dynamic_cast<T*>(&t);
}
template <typename T, typename V>
T* as(V* t) {
return dynamic_cast<T*>(t);
}

class Z {
public:
virtual void f() {};
};
class X : public Z {
public:
virtual void f() {};
};
class Y : public X {
public:
virtual void f() {};
};
class U {
public:
virtual void f() {};
};
class V : public U {
virtual void f() {};
};

int main() {
int i = 4;
int& i2 = i;
X x;
Y y;
V v;
X* x2 = &y;
X& x3 = y;

std::cout << typeof<X>() << std::endl;
std::cout << typeof<X>() << std::endl;
std::cout << typeof(x) << std::endl;
std::cout << typeof(y) << std::endl;
// std::cout << typeof<int&>() << std::endl; // needs intergral type template specialization case
// std::cout << typeof(i2) << std::endl;

std::cout << is<X>(x) << std::endl;
std::cout << is<X>(y) << std::endl;
std::cout << is<int&>(i) << std::endl; //fail
std::cout << is<int&>(i2) << std::endl; // fail

std::cout << is<Y>(x) << std::endl;
std::cout << is<X>(v) << std::endl;
std::cout << is<Y>(x2) << std::endl;
std::cout << is<Y>(x3) << std::endl; // reference upcasts pass !

//std::cout << typeid(X). << std::endl;
}

Andrey Tarasevich

unread,
Oct 21, 2022, 4:46:03 AM10/21/22
to
On 10/20/2022 11:35 PM, Aaron Gray wrote:
> template <typename T, typename V>
> bool is(V* t) {
> try {
> return dynamic_cast<T*>(t) != nullptr;
> }
> catch (std::bad_cast) {
> return false;
> }
> }
>
> template <typename T, typename V>
> bool is(V& t) {
> try {
> dynamic_cast<T&>(t);
> return true;
> }
> catch (std::bad_cast) {
> return false;
> }
> }
> template <> // FAILING !
> bool is<int&>(int t) {
> return true;
> }

You are using a template specialization syntax, but your specialization
does not match any of the previously declared "main" templates. So far
you declared two "main" templates: one that takes a pointer and one that
takes a reference. The specialization takes an `int`, which does not
match any of the "main" templates.

This will work

template <typename T>
bool is(int t){
return false;
}

template <> bool is<int&>(int t) {
return true;
}

Whether it is the best way to go about implementing your intent is a
different question...

--
Best regards,
Andrey.
0 new messages