c++ - Why does std::forward discard constexpr-ness? -
being not declared constexpr
, std::forward
discard constexpr-ness function forwards arguments to. why std::forward
not declared constexpr
can preserve constexpr-ness?
example: (tested g++ snapshot-2011-02-19)
#include <utility> template <typename t> constexpr int f(t x) { return -13;} template <typename t> constexpr int g(t&& x) { return f(std::forward<t>(x));} int main() { constexpr int j = f(3.5f); // next line not compile: // error: ‘constexpr int g(t&&) [with t = float]’ not constexpr function constexpr int j2 = g(3.5f); }
note: technically, easy make std::forward
constexpr, e.g., (note in g std::forward
has been replaced fix::forward
):
#include <utility> namespace fix { /// constexpr variant of forward, adapted <utility>: template<typename tp> inline constexpr tp&& forward(typename std::remove_reference<tp>::type& t) { return static_cast<tp&&>(t); } template<typename tp> inline constexpr tp&& forward(typename std::remove_reference<tp>::type&& t) { static_assert(!std::is_lvalue_reference<tp>::value, "template argument" " substituting tp lvalue reference type"); return static_cast<tp&&>(t); } } // namespace fix template <typename t> constexpr int f(t x) { return -13;} template <typename t> constexpr int g(t&& x) { return f(fix::forward<t>(x));} int main() { constexpr int j = f(3.5f); // compiles fine: constexpr int j2 = g(3.5f); }
my question is: why std::forward
not defined fix::forward
?
note2: question related other question constexpr std::tuple std::forward
not being constexpr
technical reason why std::tuple
cannot created calling cstr rvalues, question here (much) more general.
the general answer c++ committee's library working group have not done exhaustive trawl through working draft looking opportunities use new core facilities. these features have been used people have had time , inclination @ possible uses, there not time exhaustive checking.
there papers regarding additional uses of constexpr
in works, such in november 2010 mailing.
Comments
Post a Comment