有没有办法方便地调用模板运算符->?
在变体这样的类中有这种可能性会很酷
例如:(那只是一个例子)
struct base_t
{
template<class T>
T* operator->()
{
return reinterpret_cast<T*>(this);
}
};
int main(int argc, char* argv[])
{
base_t x;
x.operator-><std::pair<int,int>>()->first; //works, but inconvenient
x<std::pair<int,int>>->first; // does not work
x-><std::pair<int,int>>first; //does not work
return 0;
}
我需要证据 =)
不,这不是真的,这怎么也不是真的
struct base_t
{
template<class T>
T operator () ()
{
return T();
}
};
int main()
{
base_t x;
x.operator ()<int>(); // works
x.()<int>(); // not works
}
An expression x->m is interpreted as (x.operator->())->m for a class object x of type T if T::operator->()
exists and if the operator is selected as the best match function by the overload resolution mechanism
postfix-expression -> templateopt id-expression
postfix-expression -> pseudo-destructor-name
因此,语法 x-><T>
完全不正确。