我可以重载使用指针和非构造模板对象的运算符吗? [英] Can I overload an operator taking a pointer and a non-constructed template object?
问题描述
我想写一些这样的代码:
I would like to write some code like this:
some_container | foo<args...>;
具体来说,假设RHS为foo<2>
.
For concreteness, let's say the RHS is foo<2>
.
棘手的部分是,foo
是应该自动实例化的内容. IOW,如果必须显式实例化foo<2>
,或者如果必须在其后贴上()
或{}
,我将失败".
The tricky part is, foo
is something that should be automatically instantiated. IOW, I "fail" if foo<2>
has to be explicitly instantiated, or if I have to stick ()
or {}
after it.
如果LHS是类类型,我可以通过使foo
为返回标签"类型的函数,然后提供operator|
的模板重载来实现.但是,这不起作用(有关我当前使用的代码,请参见重载运算符|以获取固定大小的数组?以获取我当前使用的代码) LHS也是指针类型,如果它是C样式的数组,也是这种情况.
If the LHS is a class type, I can accomplish this by making foo
a function returning a "tag" type, then providing a templated overload of operator|
. However, this doesn't work (see Overload operator| for fixed-size arrays? for the code I am currently using) if the LHS is also a pointer type, as is the case if it is a C-style array.
在C ++ 11中是否有 的方式(即不使用C ++ 14变量模板)来实现这种语法?
Is there any way in C++11 (i.e. without using C++14 variable templates) to achieve this syntax?
该表达式的结果最终需要为:
The result of this expression ultimately needs to be :
bar<decltype(some_container), args...>{some_container}
(例如LHS int[N]
和RHS foo<2>
的bar<int (&)[N], 2>
).
(so e.g. bar<int (&)[N], 2>
for LHS int[N]
and RHS foo<2>
).
推荐答案
否.语法foo<...>
只能是类(或别名)模板(无效的表达式)或函数模板(成为函数指针,不能对其进行重载).
No. The syntax foo<...>
can only be a class (or alias) template (which is an invalid expression) or function template (which becomes a function pointer, which you can’t overload for).
这篇关于我可以重载使用指针和非构造模板对象的运算符吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!