Jump to content
  • Advertisement
Sign in to follow this  

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

This topic is 2072 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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(...);
    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!*

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

Share this post

Link to post
Share on other sites

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.)

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!