0

STL comes with many type traits like std::is_pointer, std::is_reference etc...

Lets say I have a class

class A 
{
   using type_member = void;     
}

Is there any type trait for controlling the type member and checking if it exists ?
Something like is_type_member_exist<typename A::type_member>();

I am both curious if a solution exist with C++17 and also curious about C++2003(at work I need this and I have vs2010 which has a bit C++11 support but not complete).

max66
  • 65,235
  • 10
  • 71
  • 111
Kadir Erdem Demir
  • 3,531
  • 3
  • 28
  • 39
  • 1
    Member variables and member types are different things I believe. Member types may differ since they are pure types. Language may come up with a easier solution for them. The question you directed does not helps me . – Kadir Erdem Demir Mar 24 '17 at 20:26
  • Look up `void_t`. – T.C. Mar 24 '17 at 20:33
  • Look at [`is_detected`](http://stackoverflow.com/documentation/c%2b%2b/1169/sfinae-substitution-failure-is-not-an-error/18585/is-detected). – Jarod42 Mar 24 '17 at 21:25
  • Guys is it possible for you to write a answer with an example – Kadir Erdem Demir Mar 25 '17 at 06:51
  • Is it possible for you to explain why the answer's with examples in @MooingDuck's link don't answer your question (and edit to show the difference)? – OJFord Mar 25 '17 at 10:37
  • @OJFord - sorry but... are you able to use the answer's in examples in MooingDuck's link to write an answer to this question? I'm not. – max66 Mar 25 '17 at 10:59
  • @max66 Your answer's good, and I think I understand now, I just thought the question could do with better explaining the difference of this problem. – OJFord Mar 25 '17 at 12:37
  • @OJFord - the question isn't clear and should be written better; I'm agree with you. But I think that close it because it's a duplicate of a really different question is bad. – max66 Mar 25 '17 at 12:41
  • I liked @max66 answer i am not looking for a solution but instead best alternative that is why before accepting his answer I wanted to see example with is-detected . And now sadly question marked as a duplicate – Kadir Erdem Demir Mar 25 '17 at 12:42
  • @KadirErdemDemir - yes, I'm interested in answer is_detected based too. I'm marked to reopen but you can open another question stressing that is different than asking a way to detect if, in a class/struct, it's a member (variable) or not. – max66 Mar 25 '17 at 13:05
  • @max66 I am just afraid it will be tagged duplicated again. The only problem I don't know how to ask more clearly unfortunately I stressed that it is not a member variable but member type even in the question title. I put an example with member type. I think I won't be able to learn if there is a good method for member types from stackoverflow if there is any – Kadir Erdem Demir Mar 26 '17 at 08:26
  • @max66: Example of code with `is_detected`: [Demo](http://coliru.stacked-crooked.com/a/0e3dd1d75e816413) (the first 40 lines should be unneeded with C++17) – Jarod42 Mar 27 '17 at 22:29

1 Answers1

3

If type_member is public (and not private as in your question), I suppose you can do something like

#include <iostream>

template <typename X>
struct with_type_member
 { 
   template <typename Y = X>
   static constexpr bool getValue (int, typename Y::type_member * = nullptr)
    { return true; }

   static constexpr bool getValue (long)
    { return false; }

   static constexpr bool value { getValue(0) };
 };

class A 
 {
   public:
      using type_member = void;     
 };

int main ()
 {
   std::cout << with_type_member<int>::value << std::endl; // print 0
   std::cout << with_type_member<A>::value << std::endl;   // print 1
 }

Hope this helps.

max66
  • 65,235
  • 10
  • 71
  • 111