class composition

E

er

Hi,

Is there a way to define A such that if B is defined like this

struct tag1{};
struct tag2{};
struct B : A<tag1, int>, A<tag2, double>{};

then

typedef B::F<tag1>::type v1_ // v1_ == int;
typedef B::F<tag2>::type v2_; // v2_ == double

By analogy, the above is feasible for a member function, say f,

template<typename Tag, typename T> struct A{
T f(Tag){ return T(); }
};

struct B : A<tag1, int>, A<tag2, double>
{
typedef A<tag1, int> a1_; using a1_::f;
typedef A<tag2, double> a2_; using a2_::f;

};

int main () {


B b;
b.f( tag1() );
b.f( tag2() );

return 0;

}

Thanks!
 
R

Radu

Hi,

Is there a way to define A such that if B is defined like this

    struct tag1{};
    struct tag2{};
    struct B : A<tag1, int>, A<tag2, double>{};

then

    typedef B::F<tag1>::type v1_ // v1_ == int;
    typedef B::F<tag2>::type v2_; // v2_ == double

By analogy, the above is feasible for a member function, say f,

    template<typename Tag, typename T>  struct A{
        T f(Tag){ return T(); }
    };

    struct B : A<tag1, int>, A<tag2, double>
    {
            typedef A<tag1, int> a1_; using a1_::f;
            typedef A<tag2, double> a2_; using a2_::f;

    };

int main () {

    B b;
    b.f( tag1() );
    b.f( tag2() );

    return 0;

}

Thanks!

I might be wrong, but as far as I know you can't derive from the same
type 2 times.
 
I

Ian Collins

Hi,

Is there a way to define A such that if B is defined like this

struct tag1{};
struct tag2{};
struct B : A<tag1, int>, A<tag2, double>{};

then

typedef B::F<tag1>::type v1_ // v1_ == int;
typedef B::F<tag2>::type v2_; // v2_ == double

By analogy, the above is feasible for a member function, say f,

template<typename Tag, typename T> struct A{
T f(Tag){ return T(); }
};

struct B : A<tag1, int>, A<tag2, double>
{
typedef A<tag1, int> a1_; using a1_::f;
typedef A<tag2, double> a2_; using a2_::f;

};

int main () {


B b;
b.f( tag1() );
b.f( tag2() );

return 0;

}

Thanks!

It's not clear what you are asking. The code you posted will work.
 
L

Larry Evans

Hi,

Is there a way to define A such that if B is defined like this

struct tag1{};
struct tag2{};
struct B : A<tag1, int>, A<tag2, double>{};

then

typedef B::F<tag1>::type v1_ // v1_ == int;
typedef B::F<tag2>::type v2_; // v2_ == double

By analogy, the above is feasible for a member function, say f,

template<typename Tag, typename T> struct A{
T f(Tag){ return T(); }
};

struct B : A<tag1, int>, A<tag2, double>
{
typedef A<tag1, int> a1_; using a1_::f;
typedef A<tag2, double> a2_; using a2_::f;

};
Yes. This technigue of overloading a function within
a class with a tag in order to select a the function in an
inheritance hierarchy was used here:


http://svn.boost.org/svn/boost/sand...ite_storage/layout/operators_one_of_maybe.hpp

The overloaded function was the static:

boost::composite_storage::layout::eek:perators<one_of_maybe,,,,>::
push_back<HeadLayout,TailComponent>::inject(index_part,...)

Although inject is static instead of a member function, I think
it would work for member functions also. The tag in the
above call is the index_part.

HTH.

Larry
 
E

er

It's not clear what you are asking.  The code you posted will work.

- What I'm asking is stated before I mention "By analogy".
- Only the second part of the code works. F is not even defined in the
first part.

Phrased differently, I want to be able to for meta-function, A::F<>,
the analogue of what I have shown for member-functions A::f<>().
 
E

er

Although inject is static instead of a member function, I think
it would work for member functions also.  The tag in the
above call is the index_part.

HTH.

  Larry

Thanks, but as noted in my previous message, I'm interested in the
meta-function, not the member-function part, the latter only serving
as an analogy. It seems your answer is geared towards the latter.
 
L

Larry Evans

- What I'm asking is stated before I mention "By analogy".
- Only the second part of the code works. F is not even defined in the
first part.

Phrased differently, I want to be able to for meta-function, A::F<>,
the analogue of what I have shown for member-functions A::f<>().


Your post:

http://groups.google.com/group/comp.lang.c++/msg/478fcce9739b4aa1

contained in part 1:

typedef B::F<tag1>::type v1_ // v1_ == int;
typedef B::F<tag2>::type v2_; // v2_ == double

followed by part 2:

template<typename Tag, typename T> struct A{
T f(Tag){ return T(); }
};

struct B : A<tag1, int>, A<tag2, double>
{
typedef A<tag1, int> a1_; using a1_::f;
typedef A<tag2, double> a2_; using a2_::f;

};

And the F of B::F in part 1 is the F you're referring to which is not
defined. However, I'm not sure what you're asking. Do you want A to
contained a nested F type, like:

template<typename Tag, typename T>
struct A
{
class F{
typedef T type;
}
};

such that, when B is defined as:

struct B : A<tag1, int>, A<tag2, double>
{
typedef A<tag1, int> a1_; using a1_::F;
typedef A<tag2, double> a2_; using a2_::F;

};

then:

B::F<tag1>::type would be int
B::F<tag2>::type would be double

? However, that doesn't make much sense because F is not a template,

Could you try and clarify?

-Larry
 
L

Larry Evans

- What I'm asking is stated before I mention "By analogy".
- Only the second part of the code works. F is not even defined in the
first part.

Phrased differently, I want to be able to for meta-function, A::F<>,
the analogue of what I have shown for member-functions A::f<>().
[snip]
Could you try and clarify?
Would the attached meet your needs? it compiles with
gcc5.5.1 with the -std=gnu++0x option.
 
E

er

then:

  B::F<tag1>::type would be int
  B::F<tag2>::type would be double

?  However, that doesn't make much sense because F is not a template,

Could you try and clarify?

-Larry

Thanks for following up. Yes, I want B::F<> to be a template. How to
construct it, through class composition, that is the question. Again,
I'm only giving B::f<>(), a member function, as an analogy. I was
hoping this would work

template<typename Tag,typename T>
struct A{
template<typename Tag1>
struct F : enable_if<is_same<Tag, Tag1>, T>{};
};

and within B,

using a1_::F
using a2_::F

but, of course, it would be too easy...
 
E

er

Would the attached meet your needs?  it compiles with
gcc5.5.1 with the -std=gnu++0x option.

 class_composition.cpp
< 1KViewDownload

Thanks but clicking on either of View or Download gives

Error: Failed to load and parse template
java.io.FileNotFoundException: File name: default_err.cs
Paths searched: [ ]

Do you think you could paste the code here?
 
L

Larry Evans

Would the attached meet your needs? it compiles with
gcc5.5.1 with the -std=gnu++0x option.

class_composition.cpp
< 1KViewDownload

Thanks but clicking on either of View or Download gives

Error: Failed to load and parse template
java.io.FileNotFoundException: File name: default_err.cs
Paths searched: [ ]

Do you think you could paste the code here?
Sure. Here it is:

template<typename Tag, typename T>
struct A
{
T f(Tag){ return T(); }
};

struct tag1{};
struct tag2{};
struct B : A<tag1, int>, A<tag2, double>
{
using A<tag1,int>::f;
using A<tag2,double>::f;

template<typename Tag>
struct F
;
};

template<typename Tag>
struct B::F
{
static B b;
static Tag tag;
typedef decltype(b.f(tag)) type;
};


int main () {


B b;
B::F<tag1>::type t1=b.f( tag1() );
B::F<tag2>::type t2=b.f( tag2() );

return 0;

}
 

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

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top