This code compiles in CodeGear 2009 and Visual Studio 2010, but not gcc. Why?
class Foo { public: operator int() const; template <typename T> T get() const { return this->operator T(); } }; Foo::operator int() const { return 5; }
Error message:
test.cpp: In the member function `T Foo :: get () const ':test.cpp: 6: error:" const class Foo "does not have a name named" operator T "
This is a bug in g ++. operator T- This is an unqualified dependent name (because it is in it T, and the search will differ depending on its type). Therefore, it should be considered when creating the instance. Standard rules
operator T
T
Two names are the same if..., .
Two names are the same if
, , , . , GCC
template<typename T, typename> struct identity { typedef T type; }; class Foo { public: operator int() const; template <typename T> T get() const { return this->identity<Foo, T>::type::operator T(); } };
, ++, , operator T() operator int(). :
operator T()
operator int()
template <typename T> T get() const { return static_cast<T>(*this); }
, , . , operator T(). , .
:
template <typename T> T get() const { return operator T(); }
GCC:
" T", , " " ( '-fpermissive', g++ , uneclared name )
-fmmissive, , T = int
T = int
, :
template <typename T> T get() const { return *this; }
.
, . , :
Foo f; int x = f.get<int>();
Foo f; int x = static_cast<int>(f);
, ( C), .
Source: https://habr.com/ru/post/1741840/More articles:Can protobuf-net serialize this combination of interface and shared collection? - genericsSpacebar under cursor in vim - vimDeclarative_authorization permissions for roles - ruby-on-railsNSXMLDocument objectByApplyingXSLT with XSL Include - xmlThe most efficient way to create and nest divs with appendChild using * plain * javascript (without libraries) - javascriptTrimming HTML formatted text with SMARTY - htmlC # WCF - Failed to call service - c #How to create DropShadow in UIView - iphoneObjectDisposedException when .Show () 'is a form that should not be deleted - c #Run AppleScript with elevated privileges from Objective-C - objective-cAll Articles