How to use variable template in C++

1 Answer

0 votes
#include <iostream>

template<class T>
constexpr T pi = T(3.141592653589793238L);

int main() {
    int i = pi<int>; 
    float f = pi<float>; 
    double d = pi<double>; 
   
    std::cout << i << "\n";
    std::cout << f << "\n";
    std::cout << d << "\n";
}




/*
run:

3
3.14159
3.14159

*/

 



answered Dec 6, 2022 by avibootz

Related questions

2 answers 142 views
1 answer 106 views
106 views asked Dec 6, 2022 by avibootz
1 answer 141 views
1 answer 163 views
163 views asked Dec 10, 2020 by avibootz
2 answers 178 views
178 views asked Dec 10, 2020 by avibootz
1 answer 180 views
...