3

Two methods for accessing template base class members are described here. When the base class member itself is a template, accessing it using the first method (this->) above is described here. Is there a way to use the second method (using xxxx) in this scenario as well?

For example, in the code below, is it possible to replace "????" with something to make the code work?

using namespace std;

template<typename T> class base
{
public:
    template<bool good> void foo()
    {
        cout << "base::foo<" << boolalpha << good << ">()" << endl;
    }
};

template<typename T> class derived : public base<T>
{
public:
    using ????
    void bar()
    {
        foo<true>();
    } 
};
Community
  • 1
  • 1
Aelian
  • 665
  • 1
  • 8
  • 13
  • Interesting why `using base::foo;` works initially, but then calling `foo();` ends up in a parsing error. – vsoftco Feb 05 '16 at 22:19
  • Possible duplicate of [calling template function of template base class](http://stackoverflow.com/questions/9289859/calling-template-function-of-template-base-class) – AndyG Feb 05 '16 at 22:20
  • @AndyG No, it's not a dupe, as the question is more about how to use `using` here (if possible at all). – vsoftco Feb 05 '16 at 22:21
  • I linked you to the dup, but to solve your error, you'll need to call it as `this->template foo();` – AndyG Feb 05 '16 at 22:21
  • @vsoftco: *Sheepishly* I should have followed OPs links. You're right. – AndyG Feb 05 '16 at 22:22
  • 1
    I don't think `using` can work here, because you'd need something like `using base::template foo;` but the use of the `template` keyword in `using` statements is prohibited. – AndyG Feb 05 '16 at 22:23
  • `boolalpha` forgotten to remove? – 463035818_is_not_an_ai Feb 05 '16 at 22:25
  • @tobi303 No, [`std::boolalpha`](http://en.cppreference.com/w/cpp/io/manip/boolalpha) is a stream manipulator. That's why using `std::` is better than `using namespace std;` ;) – vsoftco Feb 05 '16 at 22:26
  • 1
    @vsoftco didnt know that. Just another more reason to dislike `using namespace std;` ;) – 463035818_is_not_an_ai Feb 05 '16 at 22:27

1 Answers1

1

Just for the record, even though it does not provide any answer or workaround, but I am currently working with VS Express 2013, and I can assure you that

#include <iostream>

template<typename T> class base
{
public:
    template<bool good> void foo()
    {
        std::cout << "base::foo<" << good << ">()" << std::endl;
    }
};

template<typename T> class derived : public base<T>
{
public:

    void bar()
    {
        foo<true>();
    }
};

struct A{};

void main() {

    derived<A> a;
    a.bar();
}

works perfectly fine...

asimperial
  • 46
  • 2
  • Thanks for checking this. I am using gcc. [This](https://isocpp.org/wiki/faq/templates#nondependent-name-lookup-members) article explaining the problem mentions "... even though some compilers erroneously (temporarily?) accept it ...". Perhaps he is alluding to MSVC. – Aelian Feb 09 '16 at 16:47