-
November 3rd, 2011, 07:12 PM
#1
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?
-
November 3rd, 2011, 07:42 PM
#2
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;
}
-
November 3rd, 2011, 07:49 PM
#3
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
-
November 4th, 2011, 02:44 AM
#4
Re: Template Function for Specific Types
Originally Posted by adrian_h
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 ?
-
November 4th, 2011, 01:51 PM
#5
Re: Template Function for Specific Types
Originally Posted by superbonzo
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.
Originally Posted by superbonzo
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
-
November 5th, 2011, 05:17 PM
#6
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!
-
November 5th, 2011, 05:29 PM
#7
Re: Template Function for Specific Types
Originally Posted by IaguCool
@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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|