CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Sep 2011
    Posts
    3

    Question Template Function for Specific Types

    Hello!
    I'm pretty new to C++ but am progressing pretty fast!
    I'm constructing a class that it's non-default public constructor looks like this:
    Code:
    template<typename T, size_t N> MyArray(const T (&arrayT)[N]) {
    	//...
    }
    What I want to do is restrict the use of any type but char, int and bool.
    Like:
    Code:
    char cArr[] = {1, 2, 3, 4};
    MyArray arr(cArr); // Works fine as it should
    but
    Code:
    float fArr[] = {1.1, 2.3, 3.5, 4.2};
    MyArray arr(fArr); // It works, but I want it to throw an error since this type should not be accepted
    How can I restrict the template to be used with specific types of my choice only?

  2. #2
    Join Date
    Oct 2011
    Posts
    59

    Re: Template Function for Specific Types

    Well, if it is for just one type, exclude the T typename. For multiple, that excludes some types, use template meta programming. Boost has quite a library for this. I find it a bit difficult to navigate the documentation myself though.

    http://www.boost.org/doc/libs/1_47_0/libs/libraries.htm

    What you would be looking for is type_traits. Here is an example:

    Code:
    #include <iostream>
    #include "boost/utility.hpp"
    #include "boost/type_traits/is_same.hpp"
    using namespace std;
    
    template <class T, int i>
    void fn(T (&x)[i])
    {
    	typedef typename boost::enable_if<boost::is_same<T, int> >::type _excluder;
    	cout << i << endl;
    }
    
    int main()
    {
    	int a1[] = { 3,4,5,6,7};
    	float a2[] = { 3,4,5,6,7};
    	fn(a1); // works
    	fn(a2); // doesn't compile
    	return 0;
    }

  3. #3
    Join Date
    Oct 2011
    Posts
    59

    Re: Template Function for Specific Types

    Oh, and you would probably use boost::mpl:: or_ to check one of several types or use boost::enable_if_c with || as the intervening 'or' symbol between each boost::is_same<T, type>::value. However, IIRC, although the latter is becoming more standard, the former is more portable.


    A

  4. #4
    Join Date
    Oct 2008
    Posts
    1,456

    Re: Template Function for Specific Types

    Quote Originally Posted by adrian_h View Post
    typedef typename boost::enable_if<boost::is_same<T, int> >::type _excluder;
    usually, enable_if is used in SFINAE contexts or as a default class template parameter to enable overloads or specializations for types satisfying a predicate, it's not used as a static assert mechanism.

    indeed, use, well, static_assert instead:

    Code:
    template <class T, int i>
    void fn(T (&x)[i])
    {
    	static_assert( !std::is_same<T, float>::value, "float is not a supported type!" );
    
    	// ...
    }
    or BOOST_STATIC_ASSERT if a pre-C++11 compiler is used ...

    ...the former is more portable.
    why ?
    Last edited by superbonzo; November 4th, 2011 at 02:59 AM. Reason: added: why ?

  5. #5
    Join Date
    Oct 2011
    Posts
    59

    Re: Template Function for Specific Types

    Quote Originally Posted by superbonzo View Post
    usually, enable_if is used in SFINAE contexts or as a default class template parameter to enable overloads or specializations for types satisfying a predicate, it's not used as a static assert mechanism.

    indeed, use, well, static_assert instead:

    Code:
    template <class T, int i>
    void fn(T (&x)[i])
    {
    	static_assert( !std::is_same<T, float>::value, "float is not a supported type!" );
    
    	// ...
    }
    or BOOST_STATIC_ASSERT if a pre-C++11 compiler is used ...
    Doh! For some reason, that slipped my mind. Though, I didn't know about the static_assert in C++11, good to know.

    Quote Originally Posted by superbonzo View Post
    why ?
    IIRC, older compilers didn't deal with it correctly. Just something I heard somewhere. Think it had to do with template resolution not working correctly...


    A

  6. #6
    Join Date
    Sep 2011
    Posts
    3

    Re: Template Function for Specific Types

    @adrian_h:
    I forgot to mention that I do not use boost, I use only STD to make my own classes for my little console applications - for the sake of size.

    @superbonzo:
    Thanks a lot for the STD static_assert solution, it worked great!

  7. #7
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Template Function for Specific Types

    Quote Originally Posted by IaguCool View Post
    @adrian_h:
    I forgot to mention that I do not use boost, I use only STD to make my own classes for my little console applications - for the sake of size.
    I don't understand.

    You do know that the linker will strip out unused code, right? So size is hardly an issue.

    Regards,

    Paul McKenzie

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured