C++14 template variable and aggregate member initialization


In C++ we could only make function,class and type alias as template.But C++14 added an extension through which we can also make variables a template.

To make make a variable a template type,the normal template syntax rule applies.The syntax is shown below.

template<typename T>
T var_name= initializer ;

So applying this syntax let’s declare some template variables.

template<class T>
constexpr T var=90 ; //a constexpr template variable

template<class Ts>
Ts st=”String” ; //a normal template variable

template<typename Tc>
const Tc G=6.67e-11 //a const template variable





Some rules

If we declare a variable as template then we must declare it outside the scope of any function.This simply means we can only have a global scope template variable not local template variable.Declaring a template variable under local scope will give you an error.

Link: variables scope:local variables and global variables

Code example

template<typename Tp> Tp val=”String” ; //work fine

void func( )
{
template<class T> T var=’D’ ; //error
}

int main( )
{
template<class T> T var=890 ; //error

cin.get( );
return 0 ;
}

In Code::Blocks you will most probably get an error message as “template declaration cannot appear at block scope“.

Link:How to enable C++14 in Code::blocks?

Let’s see a program implementing template variable.

template< class T>
const T avogadro_const=6.023e23;

template< class T>
const T str=”template variable” ;

template<class T>
number_of_atoms( T mole )
{
return avogadro_const<T> * mole ;
}

int main( )
{
double mole=2;

cout<< “Number of atoms in ” << mole << ” moles ” << number_of_atoms(mole) << endl ;

cout<< str<string> ;

cin.get();
return 0;
}

Output

1.2046e+024
template variable


 


Stop wasting time,earn money($$$) from your website-Join Now!

Member initializers and aggregates

In C++11 we can assign the data member inside the structure or we can use an aggregate member initializers list to initialize the data members when an object of the structure is declared.Suppose some of the data members is initialized inside the structure and some other not,then we cannot initialize the uninitialized member with the aggregate member initializers list.

Code example:

//Work fine
struct A
{
int i=90;
char c=’B’;
string st=”String” ;
};

//Work fine
struct B
{
int i;
char c;
string st ;
};

int main( )
{
A a;

cout<< a.i << endl
<< a.c << endl
<< a.st ;

B b={ 12 , ‘B’ , “Happy” }; //aggregate initialization

cout<< a.i << endl
<< a.c << endl
<< a.st ;

return 0;
}

Output

90
B
String
12
B
Happy

The code above works since all the ‘A’ structure members are initialized inside the structure and the B members is initialized using the initialization list.

Here is another code example which gives an error.Try to notice the difference.

Code example

struct A
{
int i;
char c;
string st=”All” ; //initializer
};

int main( )
{
A a={12, ‘M’ }; ///error,aggregate initialization

return 0 ;
}

One of the data members of A is initialized inside the structure and also we try to initialize the other unitialized members using an aggregate initialization list during it’s object creation.We try to intermingle the two methods of initialization here,which is actually not allowed in C++11.

C++14 however relaxes this restriction.And so if some of the data members are not initialized inside the structure then we can initialize them using the aggregate initialization method.And also if there are fewer initializers in the initializer list than the data member is initialized using an empty initializer list.

struct A
{
int i ;
char c ;
string st=”C++14 exception” ;
};

int main( )
{
A a={ 12 , ‘A’ }; //work fine

cout<< a.i << endl
<< a.c << endl
<< a.st << endl ;

A a1={ 90 };

cout<< a.i << endl
<< a.c << endl
<< a.st << endl ;

cin.get();
return 0;
}

Output

12
A
C++14 exception
90

C++14 exception

Note for a1 object the ‘c’ data member is assigned an empty character.

In short,in C++14 we can apply both the methods of initialization in one same structure object which is not allowed in C++11.