[英]Deduce member function return type using a class member function object
編輯 :我現在有點傻。 發布的代碼實際上與??? = decltype(acc(base(i)))
...錯誤是另外一回事-雖然非常令人困惑。 我將對此發布一個新問題 。 謝謝您的幫助。
我有一些這樣的代碼:
template<typename Base, typename Acc>
struct Foo
{
Base base;
Acc acc;
auto operator()(unsigned i) const -> decltype(???) // what is ???
{ return acc(base(i)); }
};
如何獲得正確的退貨類型?
我嘗試使用decltype(acc(base(i)))
但這給出了一個錯誤(使用GCC 4.6.3):
沒有依賴於模板參數的'base'參數,因此'base'聲明必須可用
我已經為這類問題困擾了數小時或數天。 通常,gcc想要this->
但是clang不需要。 在某些情況下, Foo::
也有幫助,但是在某些情況下,我放棄了使用成員名的方式。 以下僅使用類型名稱,並且(盡管更為冗長)我認為兩者都不會有任何問題:
template<typename Base, typename Acc>
struct Foo
{
Base base;
Acc acc;
template<typename S>
using result = typename std::result_of<S>::type;
auto operator()(unsigned i) const
-> result<const Acc(result<const Base(unsigned)>)>
{ return acc(base(i)); }
};
更一般地,每當decltype(member_name)
導致的問題,可以安全地使用decltype(std::declval<member_type>())
其中member_type
是const
-qualified如果使用它的成員函數是const
。
甚至更一般地,如果您具有&&
, &
或const&
限定的成員函數,請在此類表達式中分別使用member_type
, member_type&
或const member_type&
。
我從來沒有研究過標准所說的內容,但實際上,對於類成員,編譯器在尾隨返回類型和內部函數定義中對表達式的處理方式有所不同。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.