How to correctly initialize member variable of template type?
Like so:
T a{};
Pre-C++11, this was the simplest approximation:
T a = T();
But it requires T
be copyable (though the copy is certainly going to be elided).
How do I initialize template type variables?
If T
can only be a primitive type (int
, float
, etc.), I don't think there's any difference; in all cases, the compiler will perform the relevant conversion (and will probably perform the substitution at compile-time).
If T
is a user-defined type, then obviously these won't compile, unless it has the appropriate constructors defined. At which point, it may make a difference (e.g. if you have both T::T(int)
and T::T(double)
defined).
How to initialize to zero/NULL in a template
Use Value Initialization:
T A = T(); // before C++11
T A{}; // C++11 and later
The effects of value initialization are:
1) if T is a class type with at least one user-provided constructor of any kind, the default constructor is called;
(until C++11)1) if T is a class type with no default constructor or with a user-provided or deleted default constructor, the object is default-initialized;
(since C++11)2) if T is an non-union class type without any user-provided constructors, every non-static data member and base-class component of T is value-initialized;
(until C++11)2) if T is a class type with a default constructor that is neither user-provided nor deleted (that is, it may be a class with an implicitly-defined or defaulted default constructor), the object is zero-initialized and then it is default-initialized if it has a non-trivial default constructor;
(since C++11)3) if T is an array type, each element of the array is value-initialized;
4) otherwise, the object is zero-initialized.
Unable to initialize a template variable inside a class, why?
Add missing static
. Then, either move variable definitions to namespace scope:
class foo
{
template <class T> struct null_string { static const std::string value; };
template <class T> static std::string const null_str;
};
template <class T> const std::string foo::null_string<T>::value = "";
template <class T> std::string const foo::null_str = foo::null_string<const T&>::value;
Or make them inline:
class foo
{
template <class T> struct null_string { inline static const std::string value = ""; };
template <class T> inline static const std::string null_str = foo::null_string<const T&>::value;
};
Initialization list template variable
How am I supposed to initialize the
def_value
correctly?
Simply like e.g. this:
myClass(): arr(nullptr), size(0), capacity(0), def_value() {};
// ^^^^^^^^^^^
or this:
myClass(): arr(nullptr), size(0), capacity(0), def_value{} {};
// ^^^^^^^^^^^
How to initialise vector member variable in constructor of class template
If the intent is for the vector
to have an initial size of numPeripherals
elements, then use
PeripheralSystem(uint32_t numPeripherals = 0) : peripherals(numPeripherals) {};
Now peripherals
will have numPeripherals
value-initialized (which is default initialization if T
is a class type) instances of T
.
Static member initialization in a template class
Maybe simpler
template<class T, class V>
bool fraction<T, V>::auto_reduce = true;
When you write
template<class T, class = typename enable_if<is_any_integral<T>::value>::type>
class fraction
you say that fraction
is a class with two type template paramenters; the std::enable_if
if part is useful to assign a default value to the second parameter (and to permit the enable/not enable SFINAE works) but fraction
is and remain a template class
with two parameters, you have to cite both and there is no need to repeat the enable/not enable/default part for second parameter initializing auto_reduce
.
Related Topics
How to Read Groups of Integers from a File, Line by Line in C++
Making a Template Parameter a Friend
Why Do We Need to Use 'Int Main' and Not 'Void Main' in C++
Dynamically Allocated Memory After Program Termination
Passing Const Char* as Template Argument
How to Append Text to Qplaintextedit Without Adding Newline, and Keep Scroll at the Bottom
Why Is a Char and a Bool the Same Size in C++
Cmake Find_Package Specify Path
Do I Have to Use #Include <String> Beside <Iostream>
Class Template Argument Deduction Not Working with Alias Template
Understanding Double Dispatch C++
When Is It Worthwhile to Use Bit Fields
On Local and Global Static Variables in C++
What Does It Mean for a C++ Function to Be Inline