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

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

Related

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

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!

What's an empty template for? [duplicate]

This question already has answers here:
What does a template class without arguments, `template<>` mean?
(1 answer)
In C++ what does template<> mean?
(3 answers)
Closed 1 year ago.
I was looking for how to use YAML in LLVM. And I don't understand why they use empty templates. For example, here.
template <>
struct ScalarEnumerationTraits<FooBar> {
static void enumeration(IO &io, FooBar &value) {
...
}
};
What is the template <> for?
This is not a duplicate of What is the meaning of empty "<>" in template usage?.
This is a template specialization. You will find somewhere something like:
template <typename C>
struct ScalarEnumerationTraits;
And the thing you are seeing is the specialisation of that declaration for the type FooBar.

Class template value without template arguments error [duplicate]

This question already has answers here:
What are some uses of template template parameters?
(10 answers)
Closed 2 years ago.
I have some code:
template<typename Container = std::vector> // there is error
class SomeClass {
struct SomeDataStructure {
int a;
float b;
};
Container<SomeDataStructure> container; // and there
};
I want to pass template class type in template arguments of other template class.
In this example, I want to pass std::vector to SomeClass and define Container(std::vector) of private structure SomeDataStructure.
But I'm getting errors(MSVC compiler) C2059 and C2238.
How can I solve it?
Thanks in advance!
You need template template parameters:
template<template<class, class...> class Container = std::vector>

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.