C++14 Variable Templates: what is their purpose? Any usage example?

前端 未结 6 1737
死守一世寂寞
死守一世寂寞 2020-11-29 00:52

C++14 will allow the creation of variables that are templated. The usual example is a variable \'pi\' that can be read to get the value of the mathematical constant π for va

6条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-29 01:08

    Another practical example for C++14's variable templates is when you need a function for passing something into std::accumulate:

    template
    T const & (*maxer) (T const &, T const &) = std::max;
    
    std::accumulate(some.begin(), some.end(), initial, maxer);
    

    Note that using std::max is insufficient because it can't deduce the exact signature. In this particular example you can use max_element instead, but the point is that there is a whole class of functions that share this behavior.

提交回复
热议问题