473,395 Members | 1,535 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,395 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 4312
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: 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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.