मैं "इसे स्वयं करने के लिए" का स्पष्ट समाधान दोहराऊंगा। यह कोड का succinct C ++ 11 संस्करण है, जो सरल कक्षाओं और वर्ग टेम्पलेट्स दोनों के साथ काम करता है:
#define DECLARE_SELF(Type) \
typedef Type TySelf; /**< @brief type of this class */ \
/** checks the consistency of TySelf type (calling it has no effect) */ \
void self_check() \
{ \
static_assert(std::is_same<decltype(*((TySelf*)(0))), \
decltype(*this)>::value, "TySelf is not what it should be"); \
} \
enum { static_self_check_token = __LINE__ }; \
static_assert(int(static_self_check_token) == \
int(TySelf::static_self_check_token), \
"TySelf is not what it should be")
आप इसे विचारधारा पर कार्रवाई में देख सकते हैं । उत्पत्ति, इस परिणाम के लिए अग्रणी नीचे है:
#define DECLARE_SELF(Type) typedef Type _TySelf; /**< @brief type of this class */
struct XYZ {
DECLARE_SELF(XYZ)
};
यह एक अलग वर्ग के लिए कोड को कॉपी-पेस्ट करने और XYZ को बदलने की भूल के साथ स्पष्ट समस्या है, जैसे:
struct ABC {
DECLARE_SELF(XYZ) // !!
};
मेरा पहला दृष्टिकोण बहुत मौलिक नहीं था - एक समारोह बनाना, जैसे:
/**
* @brief namespace for checking the _TySelf type consistency
*/
namespace __self {
/**
* @brief compile-time assertion (_TySelf must be declared the same as the type of class)
*
* @tparam _TySelf is reported self type
* @tparam _TyDecltypeThis is type of <tt>*this</tt>
*/
template <class _TySelf, class _TyDecltypeThis>
class CSELF_TYPE_MUST_BE_THE_SAME_AS_CLASS_TYPE;
/**
* @brief compile-time assertion (specialization for assertion passing)
* @tparam _TySelf is reported self type (same as type of <tt>*this</tt>)
*/
template <class _TySelf>
class CSELF_TYPE_MUST_BE_THE_SAME_AS_CLASS_TYPE<_TySelf, _TySelf> {};
/**
* @brief static assertion helper type
* @tparam n_size is size of object being used as assertion message
* (if it's a incomplete type, compiler will display object name in error output)
*/
template <const size_t n_size>
class CStaticAssert {};
/**
* @brief helper function for self-check, this is used to derive type of this
* in absence of <tt>decltype()</tt> in older versions of C++
*
* @tparam _TyA is reported self type
* @tparam _TyB is type of <tt>*this</tt>
*/
template <class _TyA, class _TyB>
inline void __self_check_helper(_TyB *UNUSED(p_this))
{
typedef CStaticAssert<sizeof(CSELF_TYPE_MUST_BE_THE_SAME_AS_CLASS_TYPE<_TyA, _TyB>)> _TyAssert;
// make sure that the type reported as self and type of *this is the same
}
/**
* @def __SELF_CHECK
* @brief declares the body of __self_check() function
*/
#define __SELF_CHECK \
/** checks the consistency of _TySelf type (calling it has no effect) */ \
inline void __self_check() \
{ \
__self::__self_check_helper<_TySelf>(this); \
}
/**
* @def DECLARE_SELF
* @brief declares _TySelf type and adds code to make sure that it is indeed a correct one
* @param[in] Type is type of the enclosing class
*/
#define DECLARE_SELF(Type) \
typedef Type _TySelf; /**< @brief type of this class */ \
__SELF_CHECK
} // ~self
यह एक तरह से लंबा है, लेकिन कृपया यहां मेरे साथ रहें। इसके बिना C ++ 03 में काम करने का लाभ होता है decltype, क्योंकि __self_check_helperफ़ंक्शन को प्रकार में कटौती करने के लिए नियोजित किया जाता है this। इसके अलावा, कोई भी नहीं है static_assert, लेकिन sizeof()ट्रिक इसके बजाय कार्यरत है। आप इसे C ++ 0x के लिए बहुत छोटा बना सकते हैं। अब यह टेम्प्लेट के लिए काम नहीं करेगा। इसके अलावा, मैक्रो के साथ एक छोटी सी समस्या है जो अंत में अर्धविराम की उम्मीद नहीं करता है, यदि पांडित्य के साथ संकलन किया जाता है, तो यह एक अतिरिक्त अनावश्यक अर्धविराम के बारे में शिकायत करेगा (या आप के शरीर में अर्धविराम में समाप्त नहीं होने वाले विषम दिखने वाले मैक्रो के साथ छोड़ दिया जाएगा XYZऔर ABC)।
Typeजो पास है उस पर एक चेक बनाना एक DECLARE_SELFविकल्प नहीं है, क्योंकि यह केवल XYZकक्षा की जांच करेगा (जो ठीक है), से अनजान हैABC जिसके (जिसमें त्रुटि है)। और उसके बाद उसने मुझे मारा। एक अतिरिक्त भंडारण शून्य लागत समाधान जो टेम्पलेट्स के साथ काम करता है:
namespace __self {
/**
* @brief compile-time assertion (_TySelf must be declared the same as the type of class)
* @tparam b_check is the asserted value
*/
template <bool b_check>
class CSELF_TYPE_MUST_BE_THE_SAME_AS_CLASS_TYPE2;
/**
* @brief compile-time assertion (specialization for assertion passing)
*/
template <>
class CSELF_TYPE_MUST_BE_THE_SAME_AS_CLASS_TYPE2<true> {};
/**
* @def DECLARE_SELF
* @brief declares _TySelf type and adds code to make sure that it is indeed a correct one
* @param[in] Type is type of the enclosing class
*/
#define DECLARE_SELF(Type) \
typedef Type _TySelf; /**< @brief type of this class */ \
__SELF_CHECK \
enum { __static_self_check_token = __LINE__ }; \
typedef __self::CStaticAssert<sizeof(CSELF_TYPE_MUST_BE_THE_SAME_AS_CLASS_TYPE2<int(__static_self_check_token) == int(_TySelf::__static_self_check_token)>)> __static_self_check
} // ~__self
यह केवल एक अद्वितीय एनम वैल्यू (या कम से कम यूनिक है कि आप अपने सभी कोड एक लाइन पर नहीं लिखते हैं) पर स्थैतिक अभिकथन करता है, किसी भी प्रकार की तुलना वाली प्रवंचना को नियोजित नहीं किया जाता है, और यह स्थिर असेंबल के रूप में काम करता है, यहां तक कि टेम्प्लेट में भी । और बोनस के रूप में - अंतिम अर्धविराम अब आवश्यक है :)।
मुझे एक अच्छी प्रेरणा देने के लिए मैं यक्क को धन्यवाद देना चाहता हूं। मैं उसका जवाब देखे बिना यह नहीं लिखूंगा।
वीएस 2008 और जी ++ 4.6.3 के साथ परीक्षण किया गया। वास्तव में, XYZऔर ABCउदाहरण के साथ, यह शिकायत करता है:
ipolok@ivs:~$ g++ self.cpp -c -o self.o
self.cpp:91:5: error: invalid application of âsizeofâ to incomplete type â__self::CSELF_TYPE_MUST_BE_THE_SAME_AS_CLASS_TYPE2<false>â
self.cpp:91:5: error: template argument 1 is invalid
self.cpp: In function âvoid __self::__self_check_helper(_TyB*) [with _TyA = XYZ, _TyB = ABC]â:
self.cpp:91:5: instantiated from here
self.cpp:58:87: error: invalid application of âsizeofâ to incomplete type â__self::CSELF_TYPE_MUST_BE_THE_SAME_AS_CLASS_TYPE<XYZ, ABC>â
अब अगर हम ABC को एक टेम्प्लेट बनाते हैं:
template <class X>
struct ABC {
DECLARE_SELF(XYZ); // line 92
};
int main(int argc, char **argv)
{
ABC<int> abc;
return 0;
}
हमें मिल जाएगा:
ipolok@ivs:~$ g++ self.cpp -c -o self.o
self.cpp: In instantiation of âABC<int>â:
self.cpp:97:18: instantiated from here
self.cpp:92:9: error: invalid application of âsizeofâ to incomplete type â__self::CSELF_TYPE_MUST_BE_THE_SAME_AS_CLASS_TYPE2<false>â
केवल लाइन-नंबर चेक ट्रिगर हुआ, क्योंकि फ़ंक्शन चेक संकलित नहीं किया गया था (जैसा कि अपेक्षित था)।
C ++ 0x (और बिना अंडरस्कोर के) के साथ, आपको बस आवश्यकता होगी:
namespace self_util {
/**
* @brief compile-time assertion (tokens in class and TySelf must match)
* @tparam b_check is the asserted value
*/
template <bool b_check>
class SELF_TYPE_MUST_BE_THE_SAME_AS_CLASS_TYPE;
/**
* @brief compile-time assertion (specialization for assertion passing)
*/
template <>
class SELF_TYPE_MUST_BE_THE_SAME_AS_CLASS_TYPE<true> {};
/**
* @brief static assertion helper type
* @tparam n_size is size of object being used as assertion message
* (if it's a incomplete type, compiler will display object name in error output)
*/
template <const size_t n_size>
class CStaticAssert {};
#define SELF_CHECK \
/** checks the consistency of TySelf type (calling it has no effect) */ \
void self_check() \
{ \
static_assert(std::is_same<TySelf, decltype(*this)>::value, "TySelf is not what it should be"); \
}
#define DECLARE_SELF(Type) \
typedef Type TySelf; /**< @brief type of this class */ \
SELF_CHECK \
enum { static_self_check_token = __LINE__ }; \
typedef self_util::CStaticAssert<sizeof(SELF_TYPE_MUST_BE_THE_SAME_AS_CLASS_TYPE<int(static_self_check_token) == int(TySelf::static_self_check_token)>)> static_self_check
} // ~self_util
मेरा मानना है कि CStaticAssert बिट को अभी भी आवश्यक रूप से आवश्यक है क्योंकि यह एक प्रकार का उत्पादन करता है, जो टेम्पलेट बॉडी में टाइप-एफ-एड है (मुझे लगता है कि ऐसा नहीं किया जा सकता है static_assert)। इस दृष्टिकोण का लाभ अभी भी इसकी शून्य लागत है।
this_tसंभवतया नियमित C ++ नामकरण के साथ अधिक संरेखित किया जाएगा।