473,854 Members | 1,537 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 1728
"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
6194
by: wogston | last post by:
A) template <typename scalar, int size> struct basevector { enum { size = size }; scalar v; }; B)
14
1662
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
3553
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 : y;
7
1846
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 char*,char*>.
3
5463
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
1721
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
2378
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
1778
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
14258
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
1691
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
9751
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11024
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10678
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10751
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
9512
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5940
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4557
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4153
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3185
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.