Determine if a class has a nested class defined

M

Martin

Hi.

Is it possible to determine if a certain class has a nested class/
struct defined at compile time?

E.g., we have:

class A
{
public:
class B {};
};

Write a small program, which prints out if class A has a nested class
defined, or not, of course determined at compile time.

Thanks in advance,
Martin
 
K

Kai-Uwe Bux

Martin said:
Hi.

Is it possible to determine if a certain class has a nested class/
struct defined at compile time?
Yes.

E.g., we have:

class A
{
public:
class B {};
};

Write a small program, which prints out if class A has a nested class
defined, or not, of course determined at compile time.

Sounds like homework.

Also, do you want to check for any member type / typedef? or do you insist
on B being a class. E.g., what shall the program print for

class A {
public:
typedef int B;
};


Best

Kai-Uwe Bux
 
M

Martin

Sounds like homework.

Also, do you want to check for any member type / typedef? or do you insist
on B being a class. E.g., what shall the program print for

class A {
public:
  typedef int B;

};

Best

Kai-Uwe Bux

Yes, this is a homework :) I don't insist on 'B' being just a class
type - please just bring an example, I'll catch the idea of how to
extend it for other cases :)
 
J

James Kanze

Yes, this is a homework :) I don't insist on 'B' being just a
class type - please just bring an example, I'll catch the idea
of how to extend it for other cases :)

Which is doubtlessly why Kai-Uwe didn't go into more detail. We
do occasionally help with homework, but you have to make the
first step yourself. Try looking up SFINAE. (The ultimate
source for learning templates is, of course the Vandevoorde and
Jussutis. If your course is dealing with advanced topics like
this, the professor should have already suggested it.)
 
Y

yurec

Hi

reading http://www.semantics.org/once_weakly/w02_SFINAE.pdf

i wrote this, but it seems too ugly as for me. is there better way?

#include <iostream>

class A
{
public:
class B {};
};


#define ClassHasInnerClass(Class, InnerClass) \
class Class##Has##InnerClass \
{ \
typedef char True; \
typedef struct {char test[2];} False; \
\
template <typename Class> \
static False _has(...); \
\
template <class Class> \
static True _has( typename Class::InnerClass const * ); \
\
public: \
enum {Result = (sizeof(_has<Class>(0)) == sizeof(True))}; \
};

ClassHasInnerClass(A,B);


int main()
{
if (AHasB::Result)
std::cout << "has" << std::endl;
else
std::cout << "has not" << std::endl;

getchar();
return 0;
}
 
J

James Kanze

i wrote this, but it seems too ugly as for me. is there better way?

Template metaprogramming is often a bit ugly, but not
necessarily moreso than macros:).
#include <iostream>
class A
{
public:
class B {};
};
#define ClassHasInnerClass(Class, InnerClass) \
class Class##Has##InnerClass \
{ \
typedef char True; \
typedef struct {char test[2];} False; \
\
template <typename Class> \
static False _has(...); \
\
template <class Class> \
static True _has( typename Class::InnerClass const * ); \
\
public: \
enum {Result = (sizeof(_has<Class>(0)) == sizeof(True))}; \
};

What's wrong with:

ClassHasInnerClass(A,B);

In which case, you don't need this.
int main()
{
if (AHasB::Result)

And this becomes:

if ( HasInner< A, B >::result )

(I might use macros to clean up this statement, e.g. something
like

#define hasInner(a,b) HasInner< a, b >::result

but that's about all.)
 
Y

yurec

readinghttp://www.semantics.org/once_weakly/w02_SFINAE.pdf
i wrote this, but it seems too ugly as for me. is there better way?

Template metaprogramming is often a bit ugly, but not
necessarily moreso than macros:).




#include <iostream>
class A
  {
  public:
    class B {};
  };
#define ClassHasInnerClass(Class, InnerClass)                       \
class Class##Has##InnerClass                                        \
  {                                                                 \
  typedef char True;                                                \
  typedef struct {char test[2];} False;                             \
                                                                    \
  template <typename Class>                                         \
  static False _has(...);                                           \
                                                                    \
  template <class Class>                                            \
  static True _has( typename Class::InnerClass const * );           \
                                                                    \
  public:                                                           \
    enum {Result = (sizeof(_has<Class>(0)) == sizeof(True))};       \
  };

What's wrong with:

ClassHasInnerClass(A,B);

In which case, you don't need this.
int main()
  {
  if (AHasB::Result)

And this becomes:

    if ( HasInner< A, B >::result )

(I might use macros to clean up this statement, e.g. something
like

#define hasInner(a,b) HasInner< a, b >::result

but that's about all.)
    std::cout << "has" << std::endl;
  else
    std::cout << "has not" << std::endl;
  getchar();
  return 0;
  }

--
James Kanze (GABI Software)             email:[email protected]
Conseils en informatique orientée objet/
                   Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34- Hide quoted text -

- Show quoted text -

"HasInner< A, B >::result" gives compile error 'B' : undeclared
identifier
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top