Decltype for return of a function

后端 未结 1 1872
情书的邮戳
情书的邮戳 2021-02-04 15:22

I am making a templated class that is a wrapper around any iterator. I am making the operator* this way:

template 
class MyIterator {
public:
          


        
1条回答
  •  爱一瞬间的悲伤
    2021-02-04 15:57

    This is what std::declval is for:

    decltype(*std::declval()) operator*() { /* ... */ }
    

    If your implementation does not provide std::declval (Visual C++ 2010 does not include it), you can easily write it yourself:

    template 
    typename std::add_rvalue_reference::type declval(); // no definition required
    

    Since T is an iterator type, you could also use the std::iterator_traits template, which does not require any C++0x support:

    typename std::iterator_traits::reference operator*() { /* ... */ }
    

    0 讨论(0)
提交回复
热议问题