Template Concepts doubts, from Generic Programming. C++



I am using Bjarne Stroustrup's book Programming Principles and practice.

this is from chapter 19.3 (templates) page 685, about the so called Concepts he introduces to us readers this list:

• Element(): E can be an element in a container.

• Container(): C can hold Elements and be accessed as a [begin():end()) sequence.

• Forward_iterator(): For can be used to traverse a sequence [b:e) (like a linked list, a vector, or an array).

• Input_iterator(): In can be used to read a sequence [b:e) once only (like an input stream).

• Output_iterator(): A sequence can be output using Out.

• Random_access_iterator(): Ran can be used to read and write a sequence [b:e) repeatedly and supports subscripting using [ ].

• Allocator(): A can be used to acquire and release memory (like the free store).

• Equal_comparable(): We can compare two Ts for equality using == to get a Boolean result.

• Equal_comparable(): We can compare a T to a U for equality using == to get a Boolean result.

• Predicate(): We can call P with an argument of type T to get a Boolean result.

• Binary_predicate(): We can call P with two arguments of type T to get a Boolean result.

• Binary_predicate(): We can call P with arguments of types T and U to get a Boolean result.

• Less_comparable(): We can use L to compare two Ts for less than using < to get a Boolean result.

• Less_comparable(): We can use L to compare a T to a U for less than using < to get a Boolean result.

• Binary_operation(): We can use B to do an operation on two Ts.

• Binary_operation(): We can use B to do an operation on a T and a U.

• Number(): N behaves like a number, supporting +, -, *, and /.


*you can find those pages of the book on this link if you prefer:*

https://books.google.es/books?id=We21AwAAQBAJ&pg=PA683&lpg=PA683&dq=principles+and+practice+c%2B%2B+19.3.3+Concepts&source=bl&ots=of0I42pfjR&sig=FUkUaxHzZzk2Yj5L-lA6YKTpCXA&hl=es&sa=X&ved=0ahUKEwjsisvRkbXPAhVC1hQKHcjxAP8Q6AEIMzAD#v=onepage&q=principles%20and%20practice%20c%2B%2B%2019.3.3%20Concepts&f=false

There are a few things I do not understand,

Are these defined types or recommended names he would be defining later on?

Are these only applicable through the header file he provides called stc_lib_facilities?

Does this table mean that from now on when he speaks of an "E", "C", "For" etc... on the upcoming code he is talking about something already defined within std_lib_facilities header?
Example: code from page 687 at bottom of the page

1
2
3
4
5
6
7
8
9
  template<typename T, int N> struct array {
T elem[N]; // hold elements in member array
// rely on the default constructors, destructor, and assignment
T& operator[] (int n); // access: return reference
const T& operator[] (int n) const;
T* data() { return elem; } // conversion to T*
const T* data() const { return elem; }
int size() const { return N; }
};


Is N here just the name of the int variable? or a concept? I am confused with typename's finality. Is template a reserved word?

As you can see I am overwhelmed by the whole concept and it's implementation. What is the need of "typename A = allocator<T>" part here?

1
2
3
4
5
struct out_of_range { /* . . . */ }; // class used to report range access errors
template<typename T, typename A = allocator<T>> class vector {
// . . .
T& at(int n); // checked access
const T& at(int n) const; // checked access 


and why does it keep repeating the concepts twice every time? T, typename A > T& vector<T,A

1
2
3
4
5
template<typename T, typename A > T& vector<T,A>::at(int n)
{
if (n<0 || sz<=n) throw out_of_range();
return elem[n];
}


so many questions, thanks

Kind regards
Last edited on
http://en.cppreference.com/w/cpp/language/constraints
(not part of the standard, yet)
1
2
3
void f(EqualityComparable&&); // declaration of a constrained function template
// template<typename T>
// void f(T&&) requires EqualityComparable<T>; // long form of the same 
iirc: whatever object that we pass to the `f()' function must support a `==' operation, otherwise, it will generate a compile error on the form «The type is not equality comparable»

The idea seems to be to restrict what T may be, what types you may pass to the function, making sure that it would generate a compile-time error and that it would be readable.


> Does this table mean that from now on when he speaks of an "E", "C", "For" etc...
No, those names are only valid in the definition of the concepts.
In your example `N' is an int, not any kind of number (it would fail with a float)

> What is the need of "typename A = allocator<T>"
that's a default argument.
[code]std::vector<int> v; //uses std::allocator to manage the memory
std::vector<int, my_memory_pool<int> > w; //uses my_memory_pool to manage the memory


> and why does it keep repeating the concepts twice every time?
> T, typename A > T& vector<T,A
you said that your function has two templated types, but you need to say how they are used. So, T is the return type
and you are defining a member function of the class `vector<T,A>' (note that the template parameters are part of the name, vector<int> is a different type that vector<string>)
Last edited on
Topic archived. No new replies allowed.