typeof(sttype[0].st_gid) gid() const {return st.st_gid;}
Any better solution?
Not a better solution but a conforming solution is a traits class
specialised on the particular class you wnat to find the member of and
#defined per compiler. You can make these so-called "type deduction"
schemes very sophisticated, but heres a simple example. Once the
traits class ( typeof_::member<Class, MemberTag> here) is defined, you
then use its member ::type in place of raw types. You can specialise
it on a case by case basis for implicity, though it can be made more
generic
Overall though the whole rigmarole of type deduction schemes is a pain
in the ass, and its a shame that C++ doesnt have the facitity. Its
daft because the compiler must know the type of an expression, it just
won't give it up
/*
example type deduction scheme
*/
// Only guessing !
extern "C" struct stat;
// use classes as tags to represent names
// no need to provide a definition
namespace member_tags{
struct st_gid;
}
namespace typeof_{
// declration only...
template <typename T, typename MemberTag>
struct member;
//specialise member by class and tag
// need to write and test per compiler
template<>
struct member<struct stat,member_tags::st_gid >{
#if defined(_MSC_VER)
typedef int type ; // only guessing don't know what it really
is
#elif defined(__GNUC__)
typedef short type ;
/*
...
*/
#else
#error need to define stat::st_gid for your compiler
#endif
};
}//typeof_
//Now work only in terms of the traits class
// rather than raw types...
typeof_::member<stat,member_tags::st_gid>::type my_fun()
{
typedef typeof_::member<stat,member_tags::st_gid>::type
result_type;
result_type i = 1;
return i;
}
#include <iostream>
int main()
{
std::cout << my_fun() <<'\n';
}
regards
Andy little