How to make a tuple of const references?

前端 未结 1 1350
深忆病人
深忆病人 2021-02-07 14:28

Say there are two functions:

void ff( const std::tuple ) { }

template < typename TT >
void gg( const std::tuple         


        
相关标签:
1条回答
  • 2021-02-07 14:46

    The first question is if this fits with the C++11 standard, and if it doesn't, then why?

    This is expected behaviour. In the second case template argument deduction fails because there is no T so that tuple<const T&> becomes tuple<int&>.

    In the first case it works because tuple<int&> is implicitly convertible to tuple<const int&>. This is a user-defined conversion and as such not considered during template argument deduction.

    Your questions smells a bit like an X/Y problem. Consider posting the real question that made you look for a solution involving this kind of function template/tuple combination.

    Your ctie function template looks fine. But keep in mind that things like

    auto t = ctie(5);
    

    will basically produce a dangling reference. So, you might want to restrict ctie to lvalues only.

    0 讨论(0)
提交回复
热议问题