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

Ascertaining whether T::reference exists.

I'm trying to write some class templates to ascertain whether a given
class defines a given member type.

I have been using SFINAE and the "sizeof trick" to do so and, in
general, all works well. The code snippet, below, demonstrates my
problem however. The test fails when I try and ascertain whether the
class defines a "reference" member type because I cannot declare a
pointer to a reference. Uncommenting the penultimate line of code
will result in the compiler telling me just that. Fair enough and
understood.

My question is simply "how should I proceed"?

Kind regards,
Angus
#include <iostream>
#include <vector>

typedef char One;
typedef struct { char a[2]; } Two;

template <typename T>
One has_value_type(typename T::value_type const *);

template <typename T>
Two has_value_type(...);

template <typename T>
One has_reference(typename T::reference const *);

template <typename T>
Two has_reference(...);

int main()
{
typedef std::vector<int> IntVec;

std::cout << "IntVec has value_type? "
<< (sizeof(has_value_type<IntVec>(0)) == 1)
<< std::endl;

std::cout << "IntVec has reference? "
<< (sizeof(has_reference<IntVec>(0)) == 1)
<< std::endl;

//has_value_type<IntVec>((IntVec::reference const *)0);
return 0;
}

Jul 22 '05 #1
4 1233
On Wed, 4 Feb 2004 13:48:58 +0000 (UTC), Angus Leeming
<an***********@btopenworld.com> wrote:
I'm trying to write some class templates to ascertain whether a given
class defines a given member type.

I have been using SFINAE and the "sizeof trick" to do so and, in
general, all works well. The code snippet, below, demonstrates my
problem however. The test fails when I try and ascertain whether the
class defines a "reference" member type because I cannot declare a
pointer to a reference. Uncommenting the penultimate line of code
will result in the compiler telling me just that. Fair enough and
understood.

My question is simply "how should I proceed"? template <typename T>
One has_reference(typename T::reference const *);


template <typename T>
One has_reference(typename T::reference (*)());

Tom

C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Jul 22 '05 #2
Angus Leeming wrote in news:bv**********@titan.btinternet.com:
I'm trying to write some class templates to ascertain whether a given
class defines a given member type.

I have been using SFINAE and the "sizeof trick" to do so and, in
general, all works well. The code snippet, below, demonstrates my
problem however. The test fails when I try and ascertain whether the
class defines a "reference" member type because I cannot declare a
pointer to a reference. Uncommenting the penultimate line of code
will result in the compiler telling me just that. Fair enough and
understood.

My question is simply "how should I proceed"?


#include <iostream>
#include <ostream>
#include <vector>

typedef char One;
typedef struct { char a[2]; } Two;

/* Could do with a better name :)
*/
template < typename Valid > struct has
{
typedef One type;
};

/* These 4 function's have bodies just so the last line of main
compiles and run's (i.e for testing).
*/

template <typename T>
typename has< typename T::value_type >::type has_value_type( int )
{
return One();
}

template <typename T>
Two has_value_type(...)
{
return Two();
}

template <typename T>
typename has< typename T::reference >::type has_reference( int )
{
return One();
}

template <typename T>
Two has_reference(...)
{
return Two();
}
int main()
{
typedef std::vector<int> IntVec;

std::cout << "IntVec has value_type? "
<< (sizeof(has_value_type<IntVec>(0)) == 1)
<< std::endl;

std::cout << "IntVec has reference? "
<< (sizeof(has_reference<IntVec>(0)) == 1)
<< std::endl;

std::cout << "int has reference? "
<< (sizeof(has_reference< int >(0)) == 1)
<< std::endl;

has_value_type<IntVec>(0);
return 0;
}

HTH.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #3
tom_usenet wrote:
template <typename T>
One has_reference(typename T::reference const *);
template <typename T>
One has_reference(typename T::reference (*)());


Ahhhhhh. A pointer to a function returning a reference.

Thank you, Tom.

Angus

Jul 22 '05 #4
Rob Williscroft wrote:
template < typename Valid > struct has { typedef One type; };

template <typename T>
typename has< typename T::reference >::type has_reference( int );

template <typename T>
Two has_reference(...);


The old chestnut of you can solve everything with an additional layer
of indirection.

Thanks, Rob.
Angus

Jul 22 '05 #5

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

Similar topics

21
by: scandal | last post by:
I am a javascript newbie working on a script that checks whether a "path" from one element in an array to another is "blocked." Currently, the script pushes an already processed cell index (hence...
3
by: Gareth Tonnessen | last post by:
I need to have a clean way to determine whether a query exists without trying to open it and getting an error message. Is there a simple way to determine whether an object exists in a database?...
14
by: Kayle | last post by:
How should we check if the '\0' characters exists in the string as I am confused that some books mentioned that we have to check whether we need to make sure that we pass the...
25
by: _DD | last post by:
I'd like to include a 'Test Connection' button in an app, for testing validity of a SQL connection string. I'd prefer to keep the timeout low. What is the conventional way of doing this?
4
by: ug26jhw | last post by:
Is there any way to determine whether an element in a document exists. I have some code like this where the variable i changes: if(document.getElementById('pref'+i).innerHTML... When it...
4
gundarap
by: gundarap | last post by:
Hello all, I'm working on minidom. My goal is to see whether an element already exists in the xml file before adding. I was using getElementsByTagName() to check weather the element already exists....
3
Maidenz08
by: Maidenz08 | last post by:
How do i check whether an email id exists or not? I am following a three step validation process.. 1) syntax validation- which is pretty straight forward 2) DNS validation - I'm able to do...
3
by: abhimanyu | last post by:
I have a method Marshal.IsComObject(...) that returns TRUE if an object is a COM object. I have an object of Excel.Worksheet that I released using Marshal.ReleaseComObject(...). I want to iterate...
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
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: 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
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: 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
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,...
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...

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.