#include <type_traits>
/** This is a base class that implements the watching behvaior */
class Watchable
{
// Some stuff...
} ;
/** This is a pointer that let's you observe the lifetime of types extending "Watchable" */
template < typename T>
struct WatchPtr final
{
// Some more stuff...
// Make sure that "T" extends "Watchable"
static_assert( std:: is_base_of < Watchable, T> :: value , "..." ) ;
private :
T* value;
} ;
/** For various reasons, we have chosen to forward-declare this type */
class SomeTypeThatMustBeForwardDeclared;
/** This type wants to have a WatchPtr to "SomeType..." */
class SomeOtherType
{
WatchPtr< SomeTypeThatMustBeForwardDeclared> value;
// Here you get some crazy template errors
// "But why? "SomeType..." is defined right below this!
// You don't even really need the definition at this moment anyway!
// Because fuck you, that's why.
} ;
/** Now we define it */
class SomeTypeThatMustBeForwardDeclared : public Watchable
{
// Blah blah blah
} ;
int main( ) {
// your code goes here
return 0 ;
}
I2luY2x1ZGUgPHR5cGVfdHJhaXRzPgoKLyoqIFRoaXMgaXMgYSBiYXNlIGNsYXNzIHRoYXQgaW1wbGVtZW50cyB0aGUgd2F0Y2hpbmcgYmVodmFpb3IgKi8KY2xhc3MgV2F0Y2hhYmxlCnsKCS8vIFNvbWUgc3R1ZmYuLi4KfTsKCi8qKiBUaGlzIGlzIGEgcG9pbnRlciB0aGF0IGxldCdzIHlvdSBvYnNlcnZlIHRoZSBsaWZldGltZSBvZiB0eXBlcyBleHRlbmRpbmcgIldhdGNoYWJsZSIgKi8KdGVtcGxhdGUgPHR5cGVuYW1lIFQ+CnN0cnVjdCBXYXRjaFB0ciBmaW5hbAp7CgkvLyBTb21lIG1vcmUgc3R1ZmYuLi4KCQoJLy8gTWFrZSBzdXJlIHRoYXQgIlQiIGV4dGVuZHMgIldhdGNoYWJsZSIKCXN0YXRpY19hc3NlcnQoc3RkOjppc19iYXNlX29mPFdhdGNoYWJsZSwgVD46OnZhbHVlLCAiLi4uIik7CgkKcHJpdmF0ZToKCglUKiB2YWx1ZTsKfTsKCi8qKiBGb3IgdmFyaW91cyByZWFzb25zLCB3ZSBoYXZlIGNob3NlbiB0byBmb3J3YXJkLWRlY2xhcmUgdGhpcyB0eXBlICovCmNsYXNzIFNvbWVUeXBlVGhhdE11c3RCZUZvcndhcmREZWNsYXJlZDsKCi8qKiBUaGlzIHR5cGUgd2FudHMgdG8gaGF2ZSBhIFdhdGNoUHRyIHRvICJTb21lVHlwZS4uLiIgKi8KY2xhc3MgU29tZU90aGVyVHlwZQp7CglXYXRjaFB0cjxTb21lVHlwZVRoYXRNdXN0QmVGb3J3YXJkRGVjbGFyZWQ+IHZhbHVlOyAKCS8vIEhlcmUgeW91IGdldCBzb21lIGNyYXp5IHRlbXBsYXRlIGVycm9ycwoJLy8gIkJ1dCB3aHk/ICJTb21lVHlwZS4uLiIgaXMgZGVmaW5lZCByaWdodCBiZWxvdyB0aGlzISAKCS8vIFlvdSBkb24ndCBldmVuIHJlYWxseSBuZWVkIHRoZSBkZWZpbml0aW9uIGF0IHRoaXMgbW9tZW50IGFueXdheSEKCS8vIEJlY2F1c2UgZnVjayB5b3UsIHRoYXQncyB3aHkuCn07CgovKiogTm93IHdlIGRlZmluZSBpdCAqLwpjbGFzcyBTb21lVHlwZVRoYXRNdXN0QmVGb3J3YXJkRGVjbGFyZWQgOiBwdWJsaWMgV2F0Y2hhYmxlCnsKCS8vIEJsYWggYmxhaCBibGFoCn07CgppbnQgbWFpbigpIHsKCS8vIHlvdXIgY29kZSBnb2VzIGhlcmUKCXJldHVybiAwOwp9
compilation info
In file included from prog.cpp:1:0:
/usr/include/c++/4.9/type_traits: In instantiation of 'struct std::is_base_of<Watchable, SomeTypeThatMustBeForwardDeclared>':
prog.cpp:12:1: required from 'struct WatchPtr<SomeTypeThatMustBeForwardDeclared>'
prog.cpp:29:46: required from here
/usr/include/c++/4.9/type_traits:1384:12: error: invalid use of incomplete type 'class SomeTypeThatMustBeForwardDeclared'
struct is_base_of
^
prog.cpp:24:7: error: forward declaration of 'class SomeTypeThatMustBeForwardDeclared'
class SomeTypeThatMustBeForwardDeclared;
^
prog.cpp: In instantiation of 'struct WatchPtr<SomeTypeThatMustBeForwardDeclared>':
prog.cpp:29:46: required from here
prog.cpp:12:1: error: 'value' is not a member of 'std::is_base_of<Watchable, SomeTypeThatMustBeForwardDeclared>'
{
^
stdout