473,387 Members | 1,789 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,387 software developers and data experts.

conditional typedef in templates

What I'd like to do:

template<typename T>
struct s {
typedef typename T::foo foo; // define if and only if T::foo
exists
};

Can I do something like this with templates (AFAIK not), and if not,
are there other patterns you think I should be looking at?

TIA

Mar 12 '07 #1
1 4311
n.************@gmail.com wrote:
What I'd like to do:

template<typename T>
struct s {
typedef typename T::foo foo; // define if and only if T::foo
exists
};

Can I do something like this with templates (AFAIK not), and if not,
are there other patterns you think I should be looking at?

TIA
This might not the be the best way to approach this but
this uses partial template specialization and boost::mpl
to help. Might choke some compilers.

----------------------------------------------------------
#include <vector>
#include <boost/mpl/has_xxx.hpp>

BOOST_MPL_HAS_XXX_TRAIT_DEF( iterator )

using namespace std;

template<typename T, bool enable = has_iterator<T>::value >
struct s;

template<typename T>
struct s<T, true>
{
// has foo (calling it iterator)
typedef typename T::iterator iterator;

T* m_internal;
};

template<typename T>
struct s<T, false>
{
// no foo
T* m_internal;
};
int
main()
{
typedef s< vector<int has_iter;
typedef s< int no_iter;

has_iter foo;
has_iter::iterator fooIter;
no_iter bar;

// uncomment this line to generate no a no-iterator error
//no_iter::iterator barIter;
}
Mar 12 '07 #2

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

Similar topics

5
by: Roger Leigh | last post by:
Although I've got over most of my template-related problems, I'm having trouble when I started to use default template parameters. For template type T, I've typedef'd this as object_type and then...
3
by: Generic Usenet Account | last post by:
This is a two-part question. (1) I have implemented a "Datastructure Registry" template class. I am getting no compiler warnings with older compilers, but newer compilers are generating the...
12
by: vvv | last post by:
Hi All, Do we have anything in .NET which is equivalent to C++'s Typedef . Regards, Vasanth
5
by: jimmy | last post by:
I am trying to simulate typedef template similar to the suggestion of Herb Sutter in the following article: http://www.gotw.ca/gotw/079.htm However when implementing typedef templates according...
4
by: Sacha | last post by:
I'm aware, that up to date, "typedef templates" are not defined within the C++ standard. The seemingly common workaround is this: template <class T> struct MyTypeDef { /* ultimately I need...
3
by: esuvs81 | last post by:
Hi all, i have a Vector class (as in mathematical vectors) which is templatised on two parameters, the length of the vector and it's type (int,double,etc). I am able to use a typedef like the...
5
by: Gary Wessle | last post by:
Hi I have a group of functions which have the same signature. void fun_n(void); according to a conditional structure "be it if-else or switch-case" I get to choose which one to run. ...
0
by: n.torrey.pines | last post by:
What I'd like to do: template<typename T> struct s { typedef typename T::foo foo; // define if and only if T::foo exists }; Can I do something like this with templates (AFAIK not), and if...
2
by: tropos | last post by:
I'm trying to use the 'typedef templates' idiom, mentioned in Alexandrescu's work on templates. Here's a Dr Dobbs article from 2002 that says they are a proposed C++ standard:...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
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
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
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,...

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.