473,396 Members | 1,992 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Determine equality of template parameters?

Let's say I have a function template as follows:

template<class T, class U>
foo {
...
}

However, I'd like to have different behavior if T == U.
How can I check that?
Will it work as desired if I add the following specialization?

template<class T, class T>
foo {
...
}

Thanks,
Joseph

Feb 9 '06 #1
14 3793
Joseph Turian wrote:
Let's say I have a function template as follows:

template<class T, class U>
foo {
...
}
I'm assuming this is a class template.
However, I'd like to have different behavior if T == U.
How can I check that?
Will it work as desired if I add the following specialization?

template<class T, class T>
foo {
...
}
template<class T>
class foo<T,T>
{
// ...
};

Thanks,
Joseph

Feb 9 '06 #2

red floyd wrote:
Let's say I have a function template as follows:

I'm assuming this is a class template.


No, function template.
Thanks anyway.

Joseph

Feb 9 '06 #3
BTW, my original question still stands:

Does anyone know how to do template specialization in case the two
types are equal?

Here's what I'm trying to do:
If the two types are unequal, I convert from T to U before doing stuff
with the U items.
If the two types are equal, I want to save the conversion overhead.

Thanks

Joseph

Feb 9 '06 #4
Joseph Turian wrote:
red floyd wrote:
Let's say I have a function template as follows:

I'm assuming this is a class template.


No, function template.
Thanks anyway.

Joseph


template <typename T1, typename T2>
class same_types
{
public: enum {result = false};
};

template <typename T>
class same_types<T, T>
{
public: enum {result = true};
};

template<class T, class U>
void foo(void)
{
if (same_types<T, U>::result) // same types
{
// yada yada yada
}
else // different types
{
// blah blah blah
}
}
Regards,
Ben
Feb 9 '06 #5
Joseph Turian wrote:
Let's say I have a function template as follows:

template<class T, class U>
Missing the return value type here...
foo { ^
Missing argument list here...
...
}
Are you sure you were writing a _function_ template? You do
need some arguments.

However, I'd like to have different behavior if T == U.
How can I check that?
#include <iostream>
using namespace std;

template<class T, class U> struct same { enum { yes = 0 }; };
template<class T> struct same<T,T> { enum { yes = 1 }; };

template<class T, class U> void foo()
{
if (same<T,U>::yes)
cout << "foo<T,T>\n";
else
cout << "foo<T,U>\n";
}

int main(int argc,char *argv[])
{
foo<int,double>();
foo<double,int>();
foo<char,char>();
}

Will it work as desired if I add the following specialization?

template<class T, class T>
Missing the return value type here...
foo {
^
Missing argument list here...
...
}


No partial specialisations of funciton templates allowed. See
above.

V
--
Please remove capital As from my address when replying by mail
Feb 9 '06 #6
> template <typename T1, typename T2>
class same_types
{
public: enum {result = false};
};

template <typename T>
class same_types<T, T>
{
public: enum {result = true};
};

template<class T, class U>
void foo(void)
{
if (same_types<T, U>::result) // same types
{
// yada yada yada
}
else // different types
{
// blah blah blah
}
}


Alternatively, if RTTI is enabled on your implementation then simply:

template <typename T, typename U>
void foo(void)
{
if (typeid(T) == typeid(U))
{
// blah
}
else
{
// blah
}
}

With aggressive code optimization the two version should generate
identical code, although the first version is easier to optimize (but at
the same time more verbose.)

Regards,
ben
Feb 9 '06 #7

Victor Bazarov wrote:
template<class T, class U> struct same { enum { yes = 0 }; };
template<class T> struct same<T,T> { enum { yes = 1 }; };
[...]

Can use boost::is_same to save writing above and ...

.... Can use boost::enable_if to distinguish two functions based on
predicate:

#include <iostream>
#include <boost/utility/enable_if.hpp>
#include <boost/type_traits/is_same.hpp>

using namespace std;

template<class T, class U>
typename boost::enable_if<
boost::is_same<T,U>,void::type foo()
{
cout << "foo<T,T>\n";
}

template<class T, class U>
typename boost::disable_if<
boost::is_same<T,U> // same as above
//void is default arg::type

foo()
{
cout << "foo<T,U>\n";
}

int main(int argc,char *argv[])
{
foo<int,double>();
foo<double,int>();
foo<char,char>();
}

BTW needs a reasonably good compiler though ;-)

regards
Andy Little

Feb 9 '06 #8
an**@servocomm.freeserve.co.uk wrote:
Victor Bazarov wrote:

template<class T, class U> struct same { enum { yes = 0 }; };
template<class T> struct same<T,T> { enum { yes = 1 }; };

[...]

Can use boost::is_same to save writing above and ...

[...]


Why use a third-party library when it's possible without it?
Feb 9 '06 #9
Victor Bazarov wrote:
an**@servocomm.freeserve.co.uk wrote:
Victor Bazarov wrote:

template<class T, class U> struct same { enum { yes = 0 }; };
template<class T> struct same<T,T> { enum { yes = 1 }; };

[...]

Can use boost::is_same to save writing above and ...

[...]


Why use a third-party library when it's possible without it?


Simple. It follows a standard convention. Change name of yes member to
value and your version would work with enable_if, in version above,
however though it then works in that case, in the following example I
have modified the code so that I am selecting on condition that both T
and U are the same and both are pointers. Feel free to try
uncommenting the handrolled version with changed name and using that
instead. At this point its rather more work to keep redoing all the
metafunctions. Note also that both is_same and is_pointer are scheduled
to be included in the standard library in future. Use the standard
functions because they follow standard practise and everyone knows what
their exact layout syntax and purpose is. They've been tested and
debugged and will be more portable. IOW it saves time and effort all
round.

I'm sure you knew all that though. Just accept that my way is better
for once ;-)

cheers
Andy Little
------------------

#include <iostream>
#include <boost/utility/enable_if.hpp>
#include <boost/type_traits/is_same.hpp>
#include <boost/type_traits/is_pointer.hpp>
#include <boost/mpl/and.hpp>

using namespace std;

/////////////////////////////
// hint: use value member so will work with enable_if
template<class T, class U> struct same { enum { value = 0 }; };
template<class T> struct same<T,T> { enum { value = 1 }; };
/////////////////////////////

template<class T, class U>
typename boost::enable_if<
boost::mpl::and_<
boost::is_same<T,U>,
// same<T,U>,
boost::is_pointer<T>
::type foo()
{
cout << "foo<T*,T*>\n";
}

template<class T, class U>
typename boost::disable_if<
boost::mpl::and_<
boost::is_same<T,U>,
// same<T,U>,
boost::is_pointer<T>
::type

foo()
{
cout << "foo<T,U>\n";
}

int main(int argc,char *argv[])
{
foo<int,double>();
foo<double,int>();
foo<char,char>();
foo<const char*,const char*>();
}

Feb 9 '06 #10
an**@servocomm.freeserve.co.uk wrote:
Victor Bazarov wrote:

an**@servocomm.freeserve.co.uk wrote:
Victor Bazarov wrote:

template<class T, class U> struct same { enum { yes = 0 }; };
template<class T> struct same<T,T> { enum { yes = 1 }; };
[...]

Can use boost::is_same to save writing above and ...

[...]
Why use a third-party library when it's possible without it?

Simple. It follows a standard convention.


What standard convention? Please quote the Standard document that
explains your claim about the "convention".
Change name of yes member to
value and your version would work with enable_if,
What 'enable_if'? There is no "enable_if" in the Standard.
in version above,
however though it then works in that case, in the following example I
have modified the code so that I am selecting on condition that both T
and U are the same and both are pointers. Feel free to try
uncommenting the handrolled version with changed name and using that
instead. At this point its rather more work to keep redoing all the
metafunctions. Note also that both is_same and is_pointer are scheduled
to be included in the standard library in future. Use the standard
functions
Which part of Boost is standard at this point?
because they follow standard practise and everyone knows what
their exact layout syntax and purpose is. They've been tested and
debugged and will be more portable. IOW it saves time and effort all
round.

I'm sure you knew all that though. Just accept that my way is better
for once ;-)
_What_ may be better? A third-party library? Yes, it may. But why
involve yourself with it if you don't have to?

cheers
Andy Little
------------------


V
--
Please remove capital As from my address when replying by mail
Feb 9 '06 #11

Victor Bazarov wrote:
Andy Little wrote

Simple. It follows a standard convention.


What standard convention? Please quote the Standard document that
explains your claim about the "convention".


http://www.open-std.org/jtc1/sc22/wg...2005/n1745.pdf

see sections 4.3, 4.5.1 and 4.6.
> Change name of yes member to
value and your version would work with enable_if,


What 'enable_if'? There is no "enable_if" in the Standard.


Where did I state that enable_if was in "The Standard"? enable_if is
part of the boost libraries. I thought that was clear from the
namespace prefix I gave it in the code example. Do you not know about
boost? Sorry for not spelling it out . See:

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

See http://www.boost.org for information about boost itself.
I'm sure you knew all that though. Just accept that my way is better
for once ;-)


_What_ may be better? A third-party library? Yes, it may. But why
involve yourself with it if you don't have to?


I refer you to the answers I gave to this same question in my previous
post. I suggest you answer those points Before raising further
questions, else I will assume that you are being destructive and will
simply ignore any further contributions you make in this thread.

cheers
Andy Little

Feb 9 '06 #12
an**@servocomm.freeserve.co.uk wrote:
Victor Bazarov wrote:
Andy Little wrote
Simple. It follows a standard convention.


What standard convention? Please quote the Standard document that
explains your claim about the "convention".

http://www.open-std.org/jtc1/sc22/wg...2005/n1745.pdf

see sections 4.3, 4.5.1 and 4.6.


This is not _the_Standard_. This is a _proposal_. I don't understand
why you keep missing my point. Read the beginning of the document itself
and get the paragraph 1/2 into your brain. Then we can talk.
[...]


V
--
Please remove capital As from my address when replying by mail
Feb 9 '06 #13
Victor Bazarov wrote:
an**@servocomm.freeserve.co.uk wrote:
Victor Bazarov wrote:
Andy Little wrote


Simple. It follows a standard convention.

What standard convention? Please quote the Standard document that
explains your claim about the "convention".

http://www.open-std.org/jtc1/sc22/wg...2005/n1745.pdf

see sections 4.3, 4.5.1 and 4.6.


This is not _the_Standard_. This is a _proposal_. I don't understand
why you keep missing my point. Read the beginning of the document itself
and get the paragraph 1/2 into your brain. Then we can talk.


Ok. I believe my use of the word standard has caused a
misunderstanding. I shall specifically say the C++ standard when I
mean to refer to that document otherwise I shall use the word standard
to mean common accepted use.

I'd also like to draw your attention to 1-3 in the document, which is
extremely relevant.
cheers
Andy Little

Feb 9 '06 #14

an**@servocomm.freeserve.co.uk wrote:
#include <iostream>
#include <boost/utility/enable_if.hpp>
#include <boost/type_traits/is_same.hpp>
#include <boost/type_traits/is_pointer.hpp>
#include <boost/mpl/and.hpp>


Much as I hate to interrupt a good argument, I would like to say the
following:
I've begun learning about templates only recently and I was---a few
minutes ago---trying to figure out how to get boost to do these sorts
of things. I wasn't having much luck until I read the example code in
Andy's post, which I found most helpful. So thanks.
Joseph

Feb 10 '06 #15

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

Similar topics

17
by: John Bentley | last post by:
John Bentley: INTRO The phrase "decimal number" within a programming context is ambiguous. It could refer to the decimal datatype or the related but separate concept of a generic decimal number....
3
by: Gianni Mariani | last post by:
I was a little surprised by this: It seems like the code below should not compile but the Comeau 4.3.3 compiler accepts it and the gcc 3.4(prerel) compiler rejects it and MSVC++7.1 ICE's. ...
26
by: Alexander Block | last post by:
Hello newsgroup, let's say I have a function like template<class Type> inline bool areEqual(const Type &a, const Type &b) { return ( a == b ); }
14
by: Joseph Turian | last post by:
How can I determine the type of some particular typename? I am writing a template, and it needs special case handling for some particular types: template <typename T> class foo { public:...
6
by: rincewind | last post by:
Hi, can anybody summarise all options for partial template specialization, for all kind of parameters (type, nontype, template)? I *think* I understand options for partial specialization on...
4
by: Dan Krantz | last post by:
I have the following template to ensure that a given number (val) falls into a range (between vmin & vmax): template<typename T> T ForceNumericRange( const T& val, const T& vmin, const T& vmax)...
8
by: Paul Roberts | last post by:
Hi, I'm hoping somebody here can help me with a simple problem of template syntax. Here's an example: template<typename T, int iclass A { static int a;
2
by: Kibiz0r | last post by:
Basically, I want a templated method to do one thing if the type T has a constructor that takes a std::istringstream, and another thing if it doesn't. The problem is that if I put the...
8
by: flopbucket | last post by:
Hi, I want to provide a specialization of a class for any type T that is a std::map. template<typename T> class Foo { // ... };
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...
0
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...

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.