Search code examples
c++xcodemacosdecltype

Xcode: error: expected '(' for function-style cast or type construction


I've got this error when trying to compile the following code by xcode. It is compiled without problems by Visual Studio on Windows.

template <typename OutT, typename MayaArrayT>
void DumpMayaArray(std::vector<OutT>& out, const MayaArrayT& source)
{
    using MayaElementT = decltype(MayaArrayT()[unsigned int()]); // error happens in this line!
    static_assert(std::is_same<MayaElementT, OutT&>::value, "array type mismatch");

What this code snippet is supposed to do is to get type of the element of MayaArrayT from return-type of ::operator[]( unsigned int index ); Sadly there is no other way to get type of element of MayaArrayT.


Solution

  • The code in decltype is actually a call to MayaArrayT::operator(unsigned int). However, the arguments to decltype are not actually evaluated, so what you need to do is pretend you are making the call, like this:

    using MayaElementT = decltype(
            std::declval<MayaArrayT&>()[std::declval<unsigned int>()]
          );