仅在某些情况下实例化模板中使用 decltype 的函数定义
作为理解 C++0x 的练习,我试图创建一个 C++ 类,它包装某种模板化类型的指针:
template <typename T>
class Wrapper {
T *t;
/* ... */
};
在 Wrapper 类内部,我想公开 T 可能通过 Wrapper 实现的任何重载运算符班级。包装器本身只是将函数调用转发到底层 t 对象。
template <typename U>
auto operator+(U &u) -> decltype (*t + u) {
return *t + u;
}
问题是我不希望 Wrapper 暴露 T 可能无法实现的运算符。例如,如果 T 没有实现operator+,那么Wrapper 也不应该暴露operator+。
在operator+(以及任何二元运算)的情况下,一切都会顺利进行,因为运算符必然成为模板函数,因此只有在我们尝试调用例如Wrapper::operator+ 时才会被实例化。
然而,对于一元运算符(例如,++),没有明确的方法来保护运算符,以便当且仅当 T 实现运算符++ 时才实例化它。例如,此类中operator++ 的简单实现
auto operator++() -> decltype(++(*t)) {
return ++(*t);
}
无法针对不支持operator++() 的T 进行编译。
根据我对标准的理解,如果我们有以下使用 Wrapper 的代码
class X { };
Wrapper<X> w;
,我们将实例化 Wrapper 和 Wrapper::operator++() 的声明,但不会实例化其定义,除非我们调用它(或显式实例化它)。通常这是可以的,因为 X::operator++ 的使用仅出现在 Wrapper::operator++() 的定义中。但是,由于 decltype,我们在声明中使用了 X::operator++,以便类型检查器检查 X::operator++ 是否存在,从而失败。
我们是否可以定义operator++()(以及通常使用decltype的任何此类转发函数),并使其具有实例化的属性,前提是底层对象也支持operator++()?或者考虑到模板实例化和 decltype 的语义,这是不可能实现的吗?
As an exercise in understanding C++0x, I am trying to create a C++ class that wraps a pointer of some template-ized type:
template <typename T>
class Wrapper {
T *t;
/* ... */
};
Inside of the Wrapper class, I would like to expose any overloaded operators that T may implement through the Wrapper class. The wrapper itself simply forwards the function call to the underlying t object.
template <typename U>
auto operator+(U &u) -> decltype (*t + u) {
return *t + u;
}
The catch is that I do not want Wrapper exposing operators that T may not implement. For example, if T does not implement operator+ then Wrapper should not expose operator+ as well.
In the case of operator+ (and any binary operation), everything works out because the operator necessarily becomes a template function and is thus only instantiated when we try to invoke, e.g., Wrapper::operator+.
However, in the case of unary operators (e.g., ++), there is not a clear way to guard the operator so that it is instantiated iff T implements operator++. For example, the naive implementation of operator++ in this class
auto operator++() -> decltype(++(*t)) {
return ++(*t);
}
fails to compile for a T that does not support operator++().
From my understanding of the standard, if we have the following code that uses Wrapper
class X { };
Wrapper<X> w;
We will instantiate Wrapper and the declaration of Wrapper::operator++() but not its definition unless we invoke it (or explicitly instantiate it). Normally this would be ok, because the use of X::operator++ occurs only in the definition of Wrapper::operator++(). However, because of decltype, we use X::operator++ in the declaration so that the typechecker checks for the existence of X::operator++ and thus fails.
Can we define operator++() (and in general any such forwarding function that uses decltype) with the property that it is instantiated iff the underlying object also supports operator++()? Or given the semantics of template instantiation along with decltype, is this impossible to accomplish?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以将运算符声明为非成员模板:
You can declare the operator as a non-member template:
您还可以使用默认模板参数做一些技巧,只是为了使运算符的操作数具有依赖性
,也许在中间有一个辅助函数
You could also do tricks with default template arguments, just for making the operand of the operator be dependent
Perhaps with a helper function in between
如果您能弄清楚如何将
std::enable_if
加入到运算符签名中,这里有一个元函数,用于检查例如operator->
是否存在:一些注意事项:
has_deref_opr_sfinae_impl_helper
位于has_deref_opr
内部,不知道为什么。也许这在最新版本的 GCC希望这会有所帮助。
If you can figure out how to work
std::enable_if
into the operator signature, here's a metafunction that checks for the existence of e.g.operator->
:A few notes:
has_deref_opr_sfinae_impl_helper
being inside ofhas_deref_opr
, not sure why. Maybe this is changed in more recent versions of GCCHope this helps.