如何使一个const引用的元组? [英] How to make a tuple of const references?
问题描述
说有两个函数:
void ff(const std :: tuple< const int&>){
template<类型名TT>
void gg(const std :: tuple< const TT&>){}
并调用这些函数:
int xx = 0;
ff(std :: tie(xx)); // passed
gg(std :: tie(xx)); //失败!
GCC 4.7.2无法编译最后一行,并报告错误注释:
注意:模板参数扣除/替换失败:
注意:types'const TT'和'int'具有不兼容的cv-限定符
note:'std :: tuple< int&>'不是从std :: tuple派生的< const TT&>'
第一个问题是如果这符合C ++ 11标准,如果没有,那为什么?
此外,为了克服这个问题,需要传递一个const引用的元组到
gg
,而不是传递一个非const引用的元组(std :: tie
使)。这可以通过:gg(std :: tie(std :: cref(x)));
但是,额外调用
std :: cref
是一种繁琐的事情,所以拥有ctie
这将是一个很好的const引用的元组。
第二个问题是如果需要手动写
ctie
,如果是,那么这是最好的方法吗?template< typename ... T>
std :: tuple< const T& ...> ctie(const T& ... args)
{
return std :: tie(args ...);
}
解决方案
第一个问题是如果这符合C ++ 11标准,如果没有,那为什么?
是预期的行为。在第二种情况下,模板参数推导失败,因为没有
T
,使得tuple< const T&>
code> tuple< int&> 。
在第一种情况下,它起作用,因为
tuple< int& ;>
可隐式转换为tuple< const int&>
。这是一个用户定义的转换,因此在模板参数扣除过程中不予考虑。
您的问题有点像X / Y问题。考虑发布真实问题,让您寻找涉及此类功能模板/元组组合的解决方案。
您的
ctie
函数模板看起来不错。但请记住,auto t = ctie(5)
基本上会产生悬挂引用。因此,您可能只想将
ctie
限制为lvalue。Say there are two functions:
void ff( const std::tuple<const int&> ) { } template < typename TT > void gg( const std::tuple<const TT&> ) { }
and calls to these functions:
int xx = 0; ff( std::tie( xx ) ); // passes gg( std::tie( xx ) ); // FAILS !!
GCC 4.7.2 fails to compile the last line and reports an error note like:
note: template argument deduction/substitution failed: note: types ‘const TT’ and ‘int’ have incompatible cv-qualifiers note: ‘std::tuple<int&>’ is not derived from ‘std::tuple<const TT&>’
The first question is if this fits with the C++11 standard, and if it doesn't, then why?
Furthermore, to overcome this issue one needs to pass a tuple of const references to
gg
instead of passing a tuple of non-const references (whichstd::tie
makes). This can be done by:gg( std::tie( std::cref(x) ) );
however, an extra call to
std::cref
is kind of tedious, so it would be great to have something likectie
which would make a tuple of const references.The second question is if there is a need to write
ctie
manually, and if yes, then is this the best way to do it?template < typename... T > std::tuple<const T&...> ctie( const T&... args ) { return std::tie( args... ); }
解决方案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 thattuple<const T&>
becomestuple<int&>
.In the first case it works because
tuple<int&>
is implicitly convertible totuple<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 likeauto t = ctie(5);
will basically produce a dangling reference. So, you might want to restrict
ctie
to lvalues only.这篇关于如何使一个const引用的元组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!