Quantcast
Channel: C++14 Variable Templates: what is their purpose? Any usage example? - Stack Overflow
Viewing all articles
Browse latest Browse all 7

Answer by Laurent LA RIZZA for C++14 Variable Templates: what is their purpose? Any usage example?

$
0
0

I wonder whether something along these lines would be possible: (assuming availability of template lambdas)

void some_func() {    template<typename T>    std::map<int, T> storage;    auto store = []<typename T>(int key, const T& value) { storage<T>[key] = value; };    store(0, 2);    store(1, "Hello"s);    store(2, 0.7);    // All three values are stored in a different map, according to their type. }

Now, is this useful?

As a simpler use, notice that the initialization of pi<T> uses explicit conversion (explicit call of a unary constructor) and not uniform initialization. Which means that, given a type radians with a constructor radians(double), you can write pi<radians>.


Viewing all articles
Browse latest Browse all 7

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>