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.