Boost logo

Boost :

Subject: Re: [boost] [xint] Design Question
From: Jeffrey Lee Hellrung, Jr. (jhellrung_at_[hidden])
Date: 2010-06-17 01:16:11


On 6/16/2010 10:06 PM, Chad Nelson wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> On 06/17/2010 12:00 AM, Scott McMurray wrote:
>
>>> Do you know of any way, short of making two nearly-identical copies
>>> of the integer_t class template, to provide different functions? I
>>> tried using multiple templated base classes, but the compiler
>>> seemed unable to find the inherited functions at all that way, for
>>> reasons I don't understand.
>>
>> SFINAE, through enable_if and disable_if?
>>
>> Without more details, it's hard to know what you're asking for.
>
> When I went looking for a description of policy-based design, it
> appeared to use multiple base classes, each one providing the functions
> and data specific to its own policy. But when I tried adapting my
> existing design to that, by putting the functions specific to each
> policy into their own classes, the compiler couldn't seem to find the
> functions that should have been inherited. Let me see if I can come up
> with some code to demonstrate the problem...
>
> Hm. I think I've figured it out now: for some reason, at least under
> GCC, this code would fail:
>
> - --------------------------8<------------------------------
>
> #include<iostream>
>
> using std::cout;
> using std::endl;
>
> template<bool Value>
> class Policy1 {
> public:
> void test_policy1() {
> cout<< "Policy1: "<< Value<< endl;
> }
> };
>
> template<int Value>
> class Policy2 {
> public:
> void test_policy2() {
> cout<< "Policy2: "<< Value<< endl;
> }
> };
>
> template<bool P1, int P2>
> class CombinePolicies: public Policy1<P1>, public Policy2<P2> {
> public:
> void test() {
> test_policy1();
> test_policy2();
> }
> };
>
> int main() {
> CombinePolicies<true, 3> t;
> t.test();
> }
>
> - --------------------------8<------------------------------
>
> The compiler reports "there are no arguments to 'test_policy1' that
> depend on a template parameter, so a declaration of 'test_policy1' must
> be available". Same error for test_policy2. I could call either one from
> *outside* the CombinePolicies class, but from inside, they would always
> give that error.
>
> As I said above, I think I figured out the solution. While writing that
> code just now, it occurred to me to try this...
>
> Policy1<P1>::test_policy1();
> Policy2<P2>::test_policy2();
>
> ...and it worked. But I don't understand why it failed in the first
> place. Those two function names are unique in the class, and they're
> obviously inherited because I can call them from outside the class. Why
> aren't they visible without the base class name from *within* the
> combined class?

In short: Your base classes are dependent types, so using anything from
them (functions, types, templates, etc.) will have to be explicitly
qualified (as you've discovered) or brought into the derived class'
scope with a using declaration.

In (not-so) long: For example,

http://womble.decadentplace.org.uk/c++/template-faq.html#base-lookup

- Jeff


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk