Followers 0

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

## 2 posts in this topic

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
0

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

0

##### Share on other sites
Looks good to me.
1

## Create an account

Register a new account