templates: policy-based class design and overloading operator?

R

Rui Maciel

Let's say that we have a host class foo which has part of it's interface implemented through a
policy class of type BarPolicy, which could be anyone between BarPolicy1 and BarPolicyN:

<code>

template<typename scalar>
class BarPolicy1
{
public:
BarPolicy() {};
~BarPolicy() {};

scalar value(const size_t a, const size_t b);
};

// snipped: other BarPolicy classes

template<typename scalar, template<typename> class BarPolicy>
class Foo
: public BarPolicy<scalar>
{
public:
Foo() {};
~Foo() {};


scalar value(const size_t a, const size_t b) {return 1; };
};

</code>


Now, I intend to overload operator<< to output the contents of class Foo to the standard output
streams, no matter what policy class has been used in assembling a host class Foo and no matter what
type has been passed as Foo's scalar. The thing is, overloading operator<< considering only the
case where Foo is implemented with a specific policy (for example, BarPolicy1) is a trivial task to
accomplish. Yet, I'm not getting any luck in overloading operator<< so that it accepts any generic
Foo class, independent of what policy has been adopted.

So, can anyone help? Is it possible to declare such an operator? If so, can anyone provide with an
example?


Thanks in advance,
Rui Maciel
 
R

Rui Maciel

Rui said:
Let's say that we have a host class foo which has part of it's interface
implemented through a policy class of type BarPolicy, which could be
anyone between BarPolicy1 and BarPolicyN:
<snip/>

Silly me. It appears that I've manage to get operator<< to work. The code is as follows:


<code>

template<typename scalar, template<typename> class AnyBarPolicy>
std::eek:stream & operator << ( std::eek:stream &out, Foo<scalar, AnyBarPolicy> &foo)
{
// do stuff
return out;
}

</code>


Rui Maciel
 
A

Alf P. Steinbach /Usenet

* Rui Maciel, on 08.10.2010 16:51:
Let's say that we have a host class foo which has part of it's interface implemented through a
policy class of type BarPolicy, which could be anyone between BarPolicy1 and BarPolicyN:

<code>

template<typename scalar>
class BarPolicy1
{
public:
BarPolicy() {};
~BarPolicy() {};

scalar value(const size_t a, const size_t b);
};

// snipped: other BarPolicy classes

template<typename scalar, template<typename> class BarPolicy>
class Foo
: public BarPolicy<scalar>
{
public:
Foo() {};
~Foo() {};


scalar value(const size_t a, const size_t b) {return 1; };
};

</code>


Now, I intend to overload operator<< to output the contents of class Foo to the standard output
streams, no matter what policy class has been used in assembling a host class Foo and no matter what
type has been passed as Foo's scalar. The thing is, overloading operator<< considering only the
case where Foo is implemented with a specific policy (for example, BarPolicy1) is a trivial task to
accomplish. Yet, I'm not getting any luck in overloading operator<< so that it accepts any generic
Foo class, independent of what policy has been adopted.

So, can anyone help? Is it possible to declare such an operator? If so, can anyone provide with an
example?

<code>
#include <iostream>
#include <stddef.h>

typedef ptrdiff_t Size;

template< class Scalar >
class BarPolicy1
{
public:
BarPolicy1() {};
~BarPolicy1() {};

Scalar value( Size a, Size b ) const { return 42; }
};


template< class Scalar, template< class > class BarPolicy = BarPolicy1 >
class Foo
: public BarPolicy< Scalar >
{};

template< class Scalar, template< class > class BarPolicy >
std::eek:stream& operator<<(
std::eek:stream& stream,
Foo< Scalar, BarPolicy > const& foo
)
{
return (stream << foo.value( 0, 1 ));
}

int main()
{
std::cout << Foo< int >() << std::endl;
}
</code>


Cheers & hth.,

- Alf
 
V

Victor Bazarov

Let's say that we have a host class foo which has part of it's
interface implemented through a policy class of type BarPolicy, which
could be anyone between BarPolicy1 and BarPolicyN:

<code>

template<typename scalar> class BarPolicy1 { public: BarPolicy()
{}; ~BarPolicy() {};

scalar value(const size_t a, const size_t b); };

// snipped: other BarPolicy classes

template<typename scalar, template<typename> class BarPolicy> class
Foo : public BarPolicy<scalar> { public: Foo() {}; ~Foo()
{};


scalar value(const size_t a, const size_t b) {return 1; }; };

</code>


Now, I intend to overload operator<< to output the contents of class
Foo to the standard output streams, no matter what policy class has
been used in assembling a host class Foo and no matter what type has
been passed as Foo's scalar. The thing is, overloading operator<<
considering only the case where Foo is implemented with a specific
policy (for example, BarPolicy1) is a trivial task to accomplish.
Yet, I'm not getting any luck in overloading operator<< so that it
accepts any generic Foo class, independent of what policy has been
adopted.

So, can anyone help? Is it possible to declare such an operator? If
so, can anyone provide with an example?

What do you need that operator to do?

#include <ostream>

template<class scalar, template<class> class Policy>
class UsesPolicy : public Policy<scalar>
{
friend std::eek:stream& operator << (std::eek:stream& os, UsesPolicy const&)
{
return os << "Generic output operator\n";
}
};

template<class T> class Policy1 {};
template<class T> class PolicyN {};

#include <iostream>

int main()
{
UsesPolicy<int,Policy1> up_int_P1;
UsesPolicy<char,PolicyN> up_char_PN;

std::cout << up_int_P1;
std::cout << up_char_PN;
}


V
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top