From: DeMarcus on
Hi,

To be able to initialize a member before any base class I have to
inherit the member virtually as the first base class in the
base-specifier-list. This is needed when dealing with std::streambuf.
See http://accu.org/index.php/journals/264

Basically it looks like this.

class MyOStream
: private virtual MyStreamBuf, public std::ostream
{
public:
MyOStream() : MyStreamBuf(), std::ostream( MyStreamBuf::buf() ) {}

};


Now, by means of variadic templates in C++0x I have created a universal
template that looks like this.

template<template<typename...> class Object, typename... Tn>
class InheritedComposition
{
public:

template<typename... Args>
InheritedComposition( Args... args )
: instance_( std::forward<Args>(args)... ) {}

inline Object<Tn...>& instance()
{
return instance_;
}

private:

Object<Tn...> instance_;

};


I can do this.

template<class T>
class SomeTemplate
{
};

typedef InheritedComposition<SomeTemplate> ST;


But I cannot do this.

class SomeClass
{
};

typedef InheritedComposition<SomeClass> SC;


How can I make SomeClass look like a template with zero arguments?


Thanks,
Daniel


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Paul Bibbings on
DeMarcus <use_my_alias_here(a)hotmail.com> writes:

<snip />

> But I cannot do this.
>
> class SomeClass
> {
> };
>
> typedef InheritedComposition<SomeClass> SC;
>
>
> How can I make SomeClass look like a template with zero arguments?

Does the following achieve what you want?

template< typename T = void >
class SomeClass
{ };

typedef InheritedComposition< SomeClass > SC;

I have to say that I'm not that up on variadic templates yet so I don't
know what the implications of this idea are for your wider model.

Regards

Paul Bibbings

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Francis Glassborow on
DeMarcus wrote:
> I can do this.
>
> template<class T>
> class SomeTemplate
> {
> };
>
> typedef InheritedComposition<SomeTemplate> ST;
>
>
> But I cannot do this.
>
> class SomeClass
> {
> };
>
> typedef InheritedComposition<SomeClass> SC;
>
>
> How can I make SomeClass look like a template with zero arguments?
>
>
> Thanks,
> Daniel
>

My instinct says that the answer lies in default template arguments. Now
you want the default argument to be a type you would never use anywhere
else so:

struct empty_default_type {};

template<typename T = empty_defgault_type> SomeClass {};

Just an idea to play with. I don't have the time to fleash it out and
think about the ramifications.

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: DeMarcus on
On 2010-05-24 17:45, Paul Bibbings wrote:
> DeMarcus<use_my_alias_here(a)hotmail.com> writes:
>
> <snip />
>
>> But I cannot do this.
>>
>> class SomeClass
>> {
>> };
>>
>> typedef InheritedComposition<SomeClass> SC;
>>
>>
>> How can I make SomeClass look like a template with zero arguments?
>
> Does the following achieve what you want?
>
> template< typename T = void>
> class SomeClass
> { };
>
> typedef InheritedComposition< SomeClass> SC;
>
> I have to say that I'm not that up on variadic templates yet so I don't
> know what the implications of this idea are for your wider model.
>
> Regards
>
> Paul Bibbings
>

The thing is that I don't want to touch SomeClass at all since it could
be an int. I would like to do some kind of "template cast". Like this.

typedef InheritedComposition<
template_cast< template<void> >( SomeClass ) > SC;


Or if it could be done with a specialization of InheritedComposition;
something like this (which doesn't work).

template<class Object>
class InheritedComposition
{
public:

template<typename... Args>
InheritedComposition( Args... args )
: instance_( std::forward<Args>(args)... ) {}

inline Object& instance()
{
return instance_;
}

private:

Object instance_;

};


I'm just playing around trying to learn more about templates, especially
about the variadic ones. My solution right now is the following where I
changed the class name (but I'm not satisfied using a different name).

template<class Object>
class InheritedObjectComposition
{
public:

template<typename... Args>
InheritedComposition( Args... args )
: instance_( std::forward<Args>(args)... ) {}

inline Object& instance()
{
return instance_;
}

private:

Object instance_;

};

I was wondering if there was a way to show the compiler that a class is
a template with zero arguments.

I think I found it!

This would work, right? I can't try it since gcc 4.5 doesn't support
template aliases yet.

// Original universal template.
template<template<typename...> class Object, typename... Tn>
class InheritedComposition
{
public:

template<typename... Args>
InheritedComposition( Args... args )
: instance_( std::forward<Args>(args)... ) {}

inline Object<Tn...>& instance()
{
return instance_;
}

private:

Object<Tn...> instance_;

};

// New template cast.
template<typename T>
using TemplateCast = T;

// Typedef that didn't work before now works!
typedef InheritedComposition< TemplateCast, SomeClass > SC;


What do you think? Does your compiler support template aliases yet so
you could try it out for me?


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Maxim Yegorushkin on
On 23/05/10 19:46, DeMarcus wrote:

> To be able to initialize a member before any base class I have to
> inherit the member virtually as the first base class in the
> base-specifier-list.

Note that virtual is not required, you just need your member to be the
first in the base class list.

--
Max

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

 |  Next  |  Last
Pages: 1 2
Prev: has_interface trait
Next: numeric_limits and constexpr