views:

240

answers:

4

hello everybody

Is it possible to get typename of a member variable? For example:

struct C { int value ; };

typedef typeof(C::value) type; // something like that?

Thanks

+1  A: 

May not be exactly what you're looking for, but a possibly better solution in the long run:

struct C {
  typedef int type;
  type value;
};

// now we can access the type of C::value as C::type
typedef C::type type;

This isn't exactly what you want, but it does allow us to hide the implementation type of C::value so that we can later change it, which is what I suspect you're after.

Chris Lutz
unfortunately I do not control structure, which come from external library and to not have value_type or equivalent
aaa
is it possible then that it is not supposed to be accessible?
stefanB
@stefanB library (cuda) has c interface. I am trying to make it suitable for template code.in principle I can write type traits if nothing else works.
aaa
+4  A: 

Only if you are fine with processing the type in a function

struct C { int value ; };

template<typename T, typename C>
void process(T C::*) {
  /* T is int */
}

int main() {
  process(&C::value); 
}

It won't work with reference data members. C++0x will allow decltype(C::value) to do that more easily. Not only that, but it allows decltype(C::value + 5) and any other fancy expression stuff within the decltype. Gcc4.5 already supports it.

Johannes Schaub - litb
I suppose this is the best time to ask. Do you know why they chose `decltype` over `typeof`? `typeof` is "matches" `sizeof`, `decltype` is ugly, and doesn't read as well! :(
GMan
@GMan - Random guesswork: because many compilers probably use `typeof` as a compiler-specific extension, and introducing it as a keyword would probably break a lot of code, as the standard `typeof` would inevitably be different from the compiler-specific `typeof`.
Chris Lutz
@GMan i have the same suspicion as Chris - i think it's largely a compatibility issue. As example, `decltype(C::value)` will be a different type than `decltype((C::value))`. My silly tests indicate that `typeof` on GCC yields `int` in both cases.
Johannes Schaub - litb
@Chris and litb: Ah, that makes sense. May I ask why those would be different types, and what they are? That's awfully confusing.
GMan
@GMan i agree, the rules *are* confusing, i'm afraid :) If you have a class-member access (ptr->mem or obj.mem) or a id-expression (qualified or unqualified name, like C::value or value) as operand then `decltype` will yield the type of that entity. For example, a reference will yield a reference type. If not this, then if the operand is a function call (possibly parenthesized) or an implicit call to an overloaded operator, then `decltype` will yield the return type of it (so `int const f()` and `decltype(f())` yields `int const`). ...
Johannes Schaub - litb
Johannes Schaub - litb
Yeesh D: Looks like I've got some reading to do. Thanks for explaining it.
GMan
+3  A: 

Not in C++03. C++0x introduces decltype:

typedef decltype(C::value) type;

Some compilers have a typeof extension, though:

typedef typeof(C::value) type; // gcc

If you're okay with Boost, they have a library for it:

typedef BOOST_TYPEOF(C::value) type;
GMan
The second doesn't work in g++ (to my little surprise).
pajton
Works for me. Make sure you haven't disable extensions with a switch.
GMan
A: 

It depends what you need to do with it but you would do something like:

#include <iostream>
using namespace std;

struct C
{
    typedef int VType;
    VType value;
};

int main()
{
    C::VType a = 3;
    cout << a << endl;
}
stefanB