473,545 Members | 1,998 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

template related question

Hi,

Is it possible to disable a method of a template class depending on
the typename at compile time?

thanks!
Jul 22 '05 #1
5 1712
"Mohammad" <mj*******@hotm ail.com> wrote in message
news:3d******** *************** ***@posting.goo gle.com...
Hi,

Is it possible to disable a method of a template class depending on
the typename at compile time?


In many cases it is possible to keep certain templated functions from
being considered during overload resolution, depending on properties
of the candidate template arguments. This uses the enable_if utility,
now part of boost:

http://www.boost.org/libs/utility/enable_if.html

Note that it requires compiler support for the SFINAE principle, and
therefore will not work on many old compilers, and some new ones.

Jonathan
Jul 22 '05 #2
"Jonathan Turkanis" <te******@kanga roologic.com> wrote in message:
"Mohammad" <mj*******@hotm ail.com> wrote in message
news:3d******** *************** ***@posting.goo gle.com...
Hi,

Is it possible to disable a method of a template class depending on the typename at compile time?


Sorry to reply to myself. I noticed that in your question the class is
a template, but not necessarily the member function. In this case, you
often don't need SFINAE, or even partial specialization.

Example:

struct base_with_f {
void f() { }
};

struct base_without_f { };

template<typena me T>
struct derived
: mpl::if_<
is_pointer<T>,
base_with_f,
base_without_f::type

{ };

Now, derived<T> has a member function 'f' only if T is a pointer type.
I have used the compile-time 'if' construct mpl::if_ from the Boost
Metaprogramming Library (MPL), and the traits template is_pointer from
Boost Type Traits.

Jonathan



Jul 22 '05 #3
Mohammad wrote in news:3d******** *************** ***@posting.goo gle.com:
Hi,

Is it possible to disable a method of a template class depending on
the typename at compile time?

thanks!


Yes, look for restrict_to (and enable_if)

Google groups:
http://groups.google.co.uk/groups?q=...oe=UTF-8&hl=en

boost:
http://boost-consulting.com/boost/li...enable_if.html

The other simpler way is just to intrudce an error.

template < typename > struct helper;

tempate <> struct helper< int > { typedef int type; };
tempate <> struct helper< double > { typedef int type; };
template < typename T >
struct example
{
T method()
{
typedef typename helper< T >::type T_is_wrong_type ;
return T();
}
};

any call to example< T >::method() should genarate a compile error
unless T is an int or a double.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #4
"Jonathan Turkanis" <te******@kanga roologic.com> wrote in message news:<c0******* ******@ID-216073.news.uni-berlin.de>...
"Jonathan Turkanis" <te******@kanga roologic.com> wrote in message:
"Mohammad" <mj*******@hotm ail.com> wrote in message
news:3d******** *************** ***@posting.goo gle.com...
Hi,

Is it possible to disable a method of a template class depending on the typename at compile time?
Now, derived<T> has a member function 'f' only if T is a pointer type.
I have used the compile-time 'if' construct mpl::if_ from the Boost
Metaprogramming Library (MPL), and the traits template is_pointer from
Boost Type Traits.

Jonathan

Thanks Jonathan!

What does the metaprogramming stand for? Can someone explain briefly.
Jul 22 '05 #5
"Mohammad" <mj*******@hotm ail.com> wrote...
"Jonathan Turkanis" <te******@kanga roologic.com> wrote in message

news:<c0******* ******@ID-216073.news.uni-berlin.de>...
"Jonathan Turkanis" <te******@kanga roologic.com> wrote in message:
"Mohammad" <mj*******@hotm ail.com> wrote in message
news:3d******** *************** ***@posting.goo gle.com...
> Hi,
>
> Is it possible to disable a method of a template class depending

on
> the typename at compile time?

Now, derived<T> has a member function 'f' only if T is a pointer type.
I have used the compile-time 'if' construct mpl::if_ from the Boost
Metaprogramming Library (MPL), and the traits template is_pointer from
Boost Type Traits.

Jonathan

Thanks Jonathan!

What does the metaprogramming stand for? Can someone explain briefly.


Metaprogramming is not something that can be explained briefly. Search
the web for it.

"Meta" means "in the vicinity of". If programming is making a computer
behave in a certain way based on input data, metaprogramming is making
compiler do something based on source code (as its input data). Well,
not limited to that of course...

Compile-time calculation of certain things can be given as an example
of metaprogramming . For instance, instead of making computer calculate,
say, factorials of some numbers (which can be needed often) and instead
of precalculating them yourself and putting in a table, you could write
a metaprogram using templates to calculate it:

template<unsign ed i> struct factorial {
enum { value = i * factorial<i-1>::value }; // recursive
};

// to stop the recursion from becoming infinite
template<> struct factorial<0> { enum { value = 1 }; };
template<> struct factorial<1> { enum { value = 1 }; };

int main() {
int fact10 = factorial<10>:: value; // compiler will calculate
return 0;
}

In the example above 'value' enumerator is calculated based on recursive
instantiation of templates required and utilisation of given template
specialisations for i==1 and i==0 (just in case somebody needs it). The
compiler has to precalculate the value initialisers by instantiating all
the intermediate templates but it won't have to do that during run-time,
and there is no enormous table to type (and make a mistake in).

Victor
Jul 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
6181
by: wogston | last post by:
A) template <typename scalar, int size> struct basevector { enum { size = size }; scalar v; }; B)
14
1630
by: LRS Kumar | last post by:
The following code - from "C++ Templates: The Complete Guide" by Vandevoorde/Josuttis - seems to compile with Borland C++. However it fails to compile with other compilers - Comeau Online and g++. Any idea why? template<typename T> class Shell { public: template<int N> class In {
31
3451
by: nikola | last post by:
Hi all, I was working with a simple function template to find the min of two values. But since I would like the two values to be different (type) I dont know what kind of value (type) it will return. I tried to write something like this: template <class Type1, class Type2, class Type3> Type3 findMin(Type1 x, Type2 y){ return (x < y) ? x...
7
1830
by: Siemel Naran | last post by:
Hi. I have a function template <class InputIter, class OutputIter> void f(InputIter begin, InputIter end, OutputIter result); With c of type char* and cc of type const char*, the code f(c,c,cc) calls f<char*, const char *>, which is fine. But f(c,c,c) calls a new instantiation f<char*,char*> whereas I'd like it to call f<const...
3
5445
by: bonj | last post by:
Hello How do I declare a template class such that the template class T must implement a certain interface? e.g. interface IProvideID { int MyID; };
5
1702
by: Amit | last post by:
Greetings all, I am writing some code somehwat similar to the test code I have below. I am having a variety of issues with template specialization. I am not sure if this is related to something i havent correctly understood related to template specialization or is it some problem related to the compiler. Following is the code..
2
2360
by: coolpint | last post by:
Can anyone kindly provide an explanation as to why the compiler does not "see" the function template in the contrieved code below? I think the argument deduction fails but can't figure out exactly what the problem is. Is it related to the fact that a nested class is involved? #include <iostream> using std::cout;
2
1761
by: toton | last post by:
Hi, This is a silly question related to syntax only. I have a template class with template member function, How to write it separately in a file (not translation unit, just want to separate declaration from definition! ). e.g, for ordinary template class member function it is done like, template <typename T> class Test{ public:
272
13915
by: Peter Olcott | last post by:
http://groups.google.com/group/comp.lang.c++/msg/a9092f0f6c9bf13a I think that the operator() member function does not work correctly, does anyone else know how to make a template for making two dimensional arrays from std::vectors ??? I want to use normal Array Syntax.
6
1679
by: subramanian100in | last post by:
consider the following program: #include <iostream> #include <cstdlib> using namespace std; template<typename Tvoid fcn(T arg) { cout << "from fcn(T arg)" << endl;
0
7473
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7406
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7660
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7813
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7431
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5976
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
4949
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3457
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
709
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.