[Enable-if] Selectively instantiating methods.
Hi All
This doesn't work.
template<typename T> struct S
{
typename disable_if
On Fri, Sep 23, 2011 at 11:48 PM, Igor R
I didn't test, but I think something like this should do the job:
template<typename T> struct S { template <class T1 = T> typename disable_if
::type mutating_method(); }; Default template arguments are disallowed for function templates.
True, but they are allowed in C++0x/11 (and maybe the restriction applies only to free-standing functions, not methods, didn't have time to dig up more). http://stackoverflow.com/questions/2447458/default-template-arguments-for-fu... Julien
2011/9/23 Robert Jones
Hi All
This doesn't work.
template<typename T> struct S { typename disable_if
::type mutating_method(); }; I understand why not, although only after the compiler threw it out, but obviously (in hindsight), the method itself is not a template, so can't be selectively instantiated.
The question is whether and how I can achieve the effect I'm after, ie., that the method is not present if the struct is instantiated with a const type?
You'll need to specialize S or do something to the similar effect (e.g., have two base classes and inherit from one of them depending on the template argument). Roman Perepelitsa.
On Fri, 23 Sep 2011 00:51:40 -0700, Robert Jones
Hi All
This doesn't work.
template<typename T> struct S { typename disable_if
::type mutating_method(); }; I understand why not, although only after the compiler threw it out, but obviously (in hindsight), the method itself is not a template, so can't be selectively instantiated.
The question is whether and how I can achieve the effect I'm after, ie., that the method is not present if the struct is instantiated with a const type?
Thx, Rob.
See http://thread.gmane.org/gmane.comp.lib.boost.devel/222181/focus=222402 Short answer (from the cited post):
template <class U> struct T { void f() { f_impl(
(0)); } private: template <class T> typename enable_if< mpl::and_< is_convertible
, some_condition_on<U> ::type f_impl(T) { .... } };
Basically introduce a dummy parameter with a dummy template type that's not used in f_impl. Mostafa
participants (5)
-
Igor R
-
Julien Nitard
-
Mostafa
-
Robert Jones
-
Roman Perepelitsa