Click here to Skip to main content
15,880,796 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Just to note, I cannot use the STL for this because one of the environments I'm targeting does not have a complete or accurate implementation of it.

I think I can use SFINAE to do this, but as much as it has been explained to me, I can't seem to wrap my head around it.

I have a type say:

C++
struct Foo {
    using test = char;
};

and
C++
struct Bar {
     // empty
};


I'd like to design a template type or template function such that it can detect the presence of the "test" member.

I know this is possible to do at compile time, again probably using SFINAE.

What I have tried:

I have a less elegant way to get what I want, but it's a lot of code to show here, and would require some explanation because it's pretty abstract.

The result I think would look something like this?

C++
template<typename T> static inline constexpr bool check_test(typename T::test value) {
   return true;
}
template<typename T> static inline constexpr bool check_test() {
   return false;
}


That's not right, but i don't know how to make it right. I want to check if test is declared on the class/struct.
Posted
Updated 11-Jul-21 2:21am
v3

1 solution

The type introspection example given here[^] looks for a function, but maybe you can modify it to look for a typedef or type alias by using a scope resolution operator instead. But knowing the name of the typedef tells you nothing about its underlying type, so I'm curious as to what you're trying to achieve.
 
Share this answer
 
Comments
honey the codewitch 11-Jul-21 8:21am    
I have "draw destination" classes which are loosely defined (bound via template) and have an interface/contract that includes usings. One of the members might be something called native_pixel_type and if it's present it tells me that "virtualization" is being used which means it's dithering. If it's dithering I don't want to use anti-aliasing to smooth the edges of fonts because there's not a true grayscale to work with, just black and white.
Greg Utas 11-Jul-21 8:38am    
Can't you just say that instantiations must include various has_blah functions, each of which returns a bool? To avoid invoking the function repeatedly at run time, you could save the result in a class template bool member. But maybe you don't even want the overhead of checking that. :)
honey the codewitch 11-Jul-21 11:54am    
> Can't you just say that instantiations must include various has_blah functions

In some cases, maybe if I had designed it that way. However, because things are typed to a pixel definition and other things, you can't use bool functions for every case. You have to use usings in some cases. Since I already have a using for this case, I find it inelegant to require the draw target implementer to create function to say essentially the same thing.
Greg Utas 11-Jul-21 8:44am    
Maybe https://en.cppreference.com/w/cpp/language/constraints can help. Scroll down a ways to "Type requirements". It's C++20, though.
honey the codewitch 11-Jul-21 11:55am    
I can't target C++20 unfortunately. I'm limited by the minigw gcc version used by platformIO under windows (the least up to date build platform) and that means C++14

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900