0

I am reading about the std::vector class from here. and I am a little bit confused about the template parameters used. The declaration used is:

template<
    class T
    class Allocator = std::allocator<T> 
> class vector; 

The parameter class Allocator = std::allocator<T> is confusing me.

Is this is an example of template template parameter?

I think, however, it might fit into the type-parameter-key name(optional) = default, which is Type template parameter, which I found here.

I tried an experiment with the following, but it gives a compilation error:

the value of d1 is not usable in a constant expression

#include<iostream>
#include<stdio.h>
#include<string>

using namespace std;

//Type template parameter
template<class T>
class foo{
    T a;
    public : 
    void sayHello(){
        cout<<"Say Hello to  : "<<a<<endl; //Would have taken care of the overloaading << , if there were no other errors
    }
    void  setVal(T temp){
        this->a = temp;
    }
};

class dog{
    string name ="labrador";
};

int main(){
dog d1;
//foo<d1> foo_1; ////Does not work
    return 0;
} 

How can I make the above code work?

walnut
  • 21,629
  • 4
  • 23
  • 59
warrior_monk
  • 383
  • 2
  • 14
  • 1
    Template arguments must be known at compile-time. `d1` is an object instance created at runtime. You need to change `foo` to either `foo` or `foo` since `foo` is expecting its `T` parameter to be a type, not a variable. – Remy Lebeau Feb 13 '20 at 00:59
  • 1
    Your sample won't work anyway, until you overload `operator<<` for `dog` type. And yes the instantiation should be `foo`. – aep Feb 13 '20 at 01:01
  • 2
    No, this is not a "template template parameter". – Sam Varshavchik Feb 13 '20 at 01:01
  • *Is this an example of template template parameter?* No. Template template parameters refer to template parameters that are themselves names of templates. (What a mouthful of templates) – ph3rin Feb 13 '20 at 01:02
  • 1
    I don't see how the code relates to the questions about the `std::vector` template parameters above it. Could you clarify? But your second guess is correct: The `Allocator` parameter is a type template parameter with default argument. – walnut Feb 13 '20 at 01:04
  • @aep .Did not i already mention the overloaded operator is intentially left for now ? – warrior_monk Feb 13 '20 at 01:18

1 Answers1

2

http://www.cplusplus.com/doc/oldtutorial/templates/ Simple syntax error buddy, pass a type name, not a type instance identifier. To make it compile I did a quick implement of the ostream overload.

#include<iostream>
#include<stdio.h>
#include<string>

using namespace std;

//Type template parameter
template<class T>
class foo{
    T a;
public :
    void sayHello(){
        cout<<"Say Hello to  : "<<a<<endl; //Would have taken care of the overloaading << , if there were no other errors
    }
    void  setVal(T temp){
        this->a = temp;
    }
};

class dog{
    string name ="labrador";
    friend ostream& operator<<(ostream& os, const dog& dt);
};

ostream& operator<<(ostream& os, const dog& dt)
{
    os << "dog stuff" << endl;
    return os;
}

int main(){
    dog d1;
    foo<dog> foo_1; ////Does not work
    foo_1.sayHello();
    return 0;
}