Jump to content

  • Log In with Google      Sign In   
  • Create Account

(VS2012) SFINAE solution needing a sanity check (detecting a trait added by a macro)


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 tanzanite7   Members   -  Reputation: 1379

Like
0Likes
Like

Posted 12 February 2014 - 04:21 PM

Had some really hard time to make VC2012 produce the required results. The version that finally did what i wanted (code is fairly self explanatory) is:

template<class T> class has_rc {
    typedef char yes;
    typedef long no;
    template<class S, S> struct sig; // requiring an exact signature match to exclude possibility of inheriting the trait
    template<class C> static yes test(sig<void(*)(C), &C::_rc_class_only>*);
    template<class C> static no test(...);
public:
    enum Boolean { value = sizeof(test<T>(0)) == sizeof(yes) };
};

// Macro for adding the trait. All trait related irrelevant code removed, only the trait marker for use in "has_rc".
// The "Class" is in my case auto-generated via macros, added here as a parameter instead for clarity.
#define RC(Class) public: static void _rc_class_only(Class) {}

struct TestRCnot {};
TEST(!has_rc<TestRCnot>::value);                 // value = false, no RC trait

struct TestRC { RC(TestRC); };                   // value = true, have RC trait
TEST( has_rc<TestRC>::value);

struct TestRCderived : TestRC { };               // value = false, no RC trait *trait is not inheritable!*
TEST(!has_rc<TestRCderived>::value);

struct TestRC2 : TestRCderived { RC(TestRC2); }; // value = true, have RC trait *re-adding a trait is OK*
TEST( has_rc<TestRC2>::value);
Question, can i simplify this code? Anything pointlessly complex? Would like "_rc_class_only" to be private, but it fails to compile (TestRCderived) even if i friend the "has_rc" class.

Note: can not use "constexpr".

Edited by tanzanite7, 12 February 2014 - 04:23 PM.


Sponsor:

#2 tanzanite7   Members   -  Reputation: 1379

Like
0Likes
Like

Posted 13 February 2014 - 03:45 AM

With the benefit of fresh mind / new morning - i think it is fine. My memory is a bit foggy, but i think i actually tried every alternative ... i don't think i can remove anything without it failing. I think none of the better alternatives work with VS. I'm done.

 

(PS. Good grief, Googling it was really annoying - conflicting info and massive amounts of false positives. Add to that sub-bar VS support. Really time consuming with hardly anything to show for it at the end.)



#3 King Mir   Members   -  Reputation: 2050

Like
1Likes
Like

Posted 13 February 2014 - 02:32 PM

Looks good to me.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS