lundi 26 juin 2017

Templates; constexpr ; compile time

I have several questions

1)

#include <iostream>
template<typename T>
void func(T t){}


int main()
{
 int i;
 double d;
 std::cin>>i;

if(i==1)
 func(i);
else
 func(d);
}

When (runtime/compile time) does one generate needed function? How many versions of functions are after instantiation?

2)What`s the difference between

template<typename T> auto func(T t){return 0;}

and

template<typename T> constexpr auto func(T t){return 0;}

As i understand template works at compile time and constexpr too. Why(and when) do i need to use constexpr with templates?

Aucun commentaire:

Enregistrer un commentaire