jeudi 23 février 2017

Why does universal reference not work in this case?

#include <vector>

using namespace std;

template<typename T, typename = decltype(&T::size)>
void f1(T)
{}

template<typename T, typename = decltype(&T::size)>
void f2(T&)
{}

template<typename T, typename = decltype(&T::size)>
void f3(T&&)
{}

int main()
{
    vector<int> coll;

    f1(coll); // ok
    f2(coll); // ok
    f3(coll); // error : no matching function for call to 'f3'
}

main.cpp(21,6): note: candidate template ignored: substitution failure [with T = > std::vector > &]: type 'std::vector > &' cannot be used prior to '::' because it has no members

void f3(T&&)

My compiler is clang 4.0.

To my surprise, f3(coll) is failed, while f1(coll) and f2(coll) are both ok.

Why does universal reference not work in this case?

Aucun commentaire:

Enregistrer un commentaire