What does "template class" keyword in C++ do? [duplicate] - c++

This question already has answers here:
Explicit template instantiation - when is it used?
(4 answers)
Why can templates only be implemented in the header file?
(17 answers)
Closed last month.
I am a newbie to C++ and am self-learning the databases course at CMU. In the second programming project, we implement a B+ tree index. We have a BPlusTreePage class which has two children class BPlusTreeInternalPage and BPlusTreeLeafPage. The way the skeleton code handles generic class confuses me.
In b_plus_tree_internal_page.h:
template <typename KeyType, typename ValueType, typename KeyComparator>
class BPlusTreeInternalPage : public BPlusTreePage {...};
In b_plus_tree_internal_page.cpp:
...
// valuetype for internalNode should be page id_t
template class BPlusTreeInternalPage<GenericKey<4>, page_id_t, GenericComparator<4>>;
template class BPlusTreeInternalPage<GenericKey<8>, page_id_t, GenericComparator<8>>;
template class BPlusTreeInternalPage<GenericKey<16>, page_id_t, GenericComparator<16>>;
template class BPlusTreeInternalPage<GenericKey<32>, page_id_t, GenericComparator<32>>;
template class BPlusTreeInternalPage<GenericKey<64>, page_id_t, GenericComparator<64>>;
I'm wondering what does these lines starting with template class do? In my understanding, the code should just work without those lines, but I'm not sure if I'm correct. Thanks for helping!

Related

What does <argument> mean put next to the template class name [duplicate]

This question already has answers here:
Function signature-like expressions as C++ template arguments
(2 answers)
Closed 2 years ago.
In the header file "std_function.h" there is this template class, I know what it does but I got curious about <_Res(_ArgsTypes...)>. I've never seen anywhere something like that
template<typename _Signature>
class function
template<typename _Res, typename... _ArgTypes>
class function<_Res(_ArgTypes...)>
: public _Maybe_unary_or_binary_function<_Res, _ArgTypes...>,
private _Function_base {
//...body of class
};
Can anyone explain this to me?
_Res(_ArgTypes...) is the type of a function taking _ArgTypes... as parameters and returning something of type _Res

C++ Non-Template Used as Template - Nested Template Classes [duplicate]

This question already has answers here:
Where and why do I have to put the "template" and "typename" keywords?
(8 answers)
Closed 2 years ago.
I know people have asked similar questions in this area, but I can not seem to find a solution to my exact problem. I have a LinkedList and LinkedListIterator class structured as so.
template <typename T>
class LinkedList
{
public:
template <typename NodeType>
class LinkedListIterator
{
public:
const LinkedListIterator<NodeType>& operator++();
}
};
I am trying to implement the operator++() function in a separate file and my attempt is as follows.
template <typename T>
template <typename NodeType>
const typename LinkedList<T>::LinkedListIterator<NodeType>&
LinkedList<T>::LinkedListIterator<NodeType>::operator++()
{
// Implementation here
}
Now when I try to use this class, my compiler throws an error saying non-template ‘LinkedListIterator’ used as template. Now I know I have to use typename in the definition of this function since I have dependent scopes occurring. But I can not seem to be able to fix this. I do not understand how this is seen as non-template.
Add template keyword before LinkedListIterator so compiler knows it is a template and not e.g. a field
template <typename T>
template <typename NodeType>
const typename LinkedList<T>::template LinkedListIterator<NodeType>&
LinkedList<T>::LinkedListIterator<NodeType>::operator++()
{
// Implementation here
}

C++11 template: How to ensure that the type inherits a class? [duplicate]

This question already has answers here:
How to force template <class> to be derived from BaseClassA?
(3 answers)
Closed 2 years ago.
In Java generic, when I want to ensure that the type of some generic class must inherit a class, I can code as below:
public class MyHandler<T extends Serializable> {}
This means that T must extend/implement the class/interface Serializable, otherwise, the compiler will generate an error.
How to get the same thing in C++11? I mean, if I code C++11 as below:
template<typename T>
class MyHandler {}
In this piece of code, T can be any class. But, if I want to tell the class users that T must inherit the class boost::archive::text_oarchive (just like <T extends Serializable> in Java), what can I do?
You can use std::is_base_of for checking.
template<typename T>
class MyHandler {
static_assert(std::is_base_of<boost::archive::text_oarchive, T>::value, "T must inherit boost::archive::text_oarchive");
};
Since C++20 we can use constraint, e.g.
template<typename T> requires std::is_base_of_v<boost::archive::text_oarchive, T>
class MyHandler {};

Derive from class template which is nested in class template, all withing a class template [duplicate]

This question already has answers here:
Where and why do I have to put the "template" and "typename" keywords?
(8 answers)
Closed 3 years ago.
I'm trying to get this to work:
template<template<typename> class... Attributes>
struct Skills
{
template<class DERIVED>
struct Create : public Attributes<DERIVED>...
{};
};
template <class T, class SKILLS>
class Tester : public typename SKILLS::Create<Tester<T,SKILLS>>
{}
but the compiler complains with:
error C2518: keyword 'typename' illegal in base class list; ignored
This works fine if I don't derive within a class template, however.
Is there any chance to derive from the nested template class?
Why do I need that? I'm trying to implement a nicer interface for a CRTP-class template, which allows doing something like:
using MyClass = ClassWithAttributes<Skills<Plus,Minus,Equal,Clear,Size>>
You have to write
template <class T, class SKILLS>
class Tester : public SKILLS::template Create<Tester<T,SKILLS>>
{ };
I mean
(1) no typename: in the list of base classes is implicit that the arguments are types
(2) you need template, before Create
(3) use SKILLS as argument of Tester, not Skills, because you have declared the template parameter SKILLS
(4) remember the semicolon at the end of class definition

Questions about template and typename [duplicate]

This question already has answers here:
Where and why do I have to put the "template" and "typename" keywords?
(8 answers)
Closed 3 years ago.
I found this article:
https://tristanbrindle.com/posts/beware-copies-initializer-list
and I have some question about part of the presented code:
First:
I think that is something about dependent name but I am not sure what is a purpouse of this part:
template <typename T>
typename instance_counter<T>::counter instance_counter<T>::icounter{};
And second one:
template <typename T>
struct counted : T, private instance_counter<T>
{
using T::T;
};
Could somebody give me an explanation about these codes?
Could somebody give me an explanation about these codes?
template <typename T>
typename instance_counter<T>::counter instance_counter<T>::icounter{};
This is the initialization of a static variable declared in the instance_counter template (see the static struct counter { /* ... */ } icounter; in the blog post). See this thread for more info on the initialization of static data members. The definition refers to a nested name of a template (counter), and the compiler defaults to consider it a name for a value, not a type. To change this default interpretation, you need to prepend typename. See this thread for details.
template <typename T>
struct counted : T, private instance_counter<T>
{
using T::T;
};
Here, the template counted inherits publicly from T (public inheritance is the default for structs) and privately from instance_counter<T>. The public inheritance part together with using T::T (which brings in all ctor overloads of T) is used to provide the same interface as the class the template is instantiated with (e.g. string in the post). The private inheritance part means is-implemented-in-terms-of and makes sure the instance_counter machinery is pulled in for the newly instantiated class type, with the output being generated in the destructor.