[enable_if] How to enable a constructor

Hi all, I am playing around with enable_if and I want to create a constructor switch. The code is the following: ------------------------------------------------ struct NullType{}; struct TestType{}; struct NonNull{}; template<typename T, typename U = NullType> struct TemplateStruct { TemplateStruct(int i, typename boost::enable_if<boost::is_same<U, NullType>, void* >::type dummy = 0) { std::cout << "One Param == " << i << std::endl; } TemplateStruct(int i, int j, typename boost::disable_if<boost::is_same<U, NullType>, void* >::type dummy = 0) { std::cout << "Two Param == " << i << "," << j << std::endl; } }; int main(int /*argc*/, char**) { TemplateStruct<TestType>(1); TemplateStruct<TestType,NonNull>(1,2); return 0; } ------------------------------------- I want that the first Ctor is only available when a NullType is passed in and if not that the second one is available but not the first. I don't want to do a class specialization because the class is rather big and extracting all the methods to a common class would be difficult. So how can I reuse the template parameter U for the Ctors enable_if? Or is this simply not possible? Regards, Michael

On 29/03/2011 6:51, Michael Kaes wrote:
So how can I reuse the template parameter U for the Ctors enable_if? Or is this simply not possible? You can't, since U is not a template parameter for the constructors but for the class itself. A template specialization is the way to do it.
K-ballo.-

On Tue, Mar 29, 2011 at 11:51 AM, Michael Kaes <michael.kaes@gmail.com>wrote:
Hi all,
I am playing around with enable_if and I want to create a constructor switch. The code is the following:
------------------------------------------------ struct NullType{}; struct TestType{}; struct NonNull{};
template<typename T, typename U = NullType> struct TemplateStruct { TemplateStruct(int i, typename boost::enable_if<boost::is_same<U, NullType>, void* >::type dummy = 0) { std::cout << "One Param == " << i << std::endl; }
TemplateStruct(int i, int j, typename boost::disable_if<boost::is_same<U, NullType>, void* >::type dummy = 0) { std::cout << "Two Param == " << i << "," << j << std::endl; } };
int main(int /*argc*/, char**) { TemplateStruct<TestType>(1); TemplateStruct<TestType,NonNull>(1,2); return 0; } -------------------------------------
I want that the first Ctor is only available when a NullType is passed in and if not that the second one is available but not the first. I don't want to do a class specialization because the class is rather big and extracting all the methods to a common class would be difficult. So how can I reuse the template parameter U for the Ctors enable_if? Or is this simply not possible?
Regards, Michael
Hi! Here is the answer to your question. I had a similar problem. http://lists.boost.org/boost-users/2007/11/31849.php <http://lists.boost.org/boost-users/2007/11/31849.php>With Kind Regards, Ovanes

On 3/29/2011 2:51 AM, Michael Kaes wrote:
I want that the first Ctor is only available when a NullType is passed in and if not that the second one is available but not the first. I don't want to do a class specialization because the class is rather big and extracting all the methods to a common class would be difficult. So how can I reuse the template parameter U for the Ctors enable_if? Or is this simply not possible?
For cases like this you do not actually need to enable/disable your constructor using enable_if or friends. Remember, a member of a template class is not instantiated if it is not used. Thus... struct nil_ {}; struct test {}; struct non_nil {}; #include <type_traits> template < typename T, typename U = nil_ > struct tstruct { tstruct(int) { static_assert( std::is_same<U,nil_>::value, "U must be nil." ); } tstruct(int,int) { static_assert( !std::is_same<U,nil_>::value, "U must not be nil." ); } }; int main() { tstruct<test>(1); tstruct<test,non_nil>(1,2); tstruct<test>(1,2); // C2338: U must not be nil. } The first two lines work fine and the program compiles without the third. The third though results in error. You won't be able to write metafunctions to test for the availability of such functions since they are, in fact, available...but if all you really want is to disallow certain functions when the instantiation types match some criteria then this does it.
participants (4)
-
Agustín Bergé
-
Michael Kaes
-
Noah Roberts
-
Ovanes Markarian