0

I expect this code will work with vector of vector but it seems to detect the existence of size only for the external vector. Anyone can help?

    #include <iostream>
#include <vector>
 

// SFINAE test
template <typename T>
class has_size
{
    typedef char one;
    struct two { char x[2]; };

    template <typename C> static one test( decltype(&C::size) ) ;
    template <typename C> static two test(...);    

public:
    enum { value = sizeof(test<T>(0)) == sizeof(char) };
};


 
    
int main(int argc, char *argv[])
{
  
    float x=2;
    std::cout << typeid(x).name() << '\n';
    std::cout << has_size<decltype(x)>::value << std::endl;
    std::vector<std::vector<std::vector<float>>> y{};
    std::cout << typeid(y).name() << '\n';
    std::cout << has_size<decltype(y)>::value << std::endl;
    std::cout << typeid(y.at(0)).name() << '\n';
    std::cout << has_size<decltype(y.at(0))>::value << std::endl;
    std::cout << typeid(y.at(0).at(0)).name() << '\n';
    std::cout << has_size<decltype(y.at(0).at(0))>::value << std::endl;
    return 0;
}

Below you can find the output:

f 0

St6vectorIS_IS_IfSaIfEESaIS1_EESaIS3_EE 1 

St6vectorIS_IfSaIfEESaIS1_EE 0 

St6vectorIfSaIfEE 0
Jason
  • 36,170
  • 5
  • 26
  • 60
Fzza
  • 105
  • 1
  • 9

1 Answers1

0

Your tests are wrong:

decltype(y.at(0)) is actually std::vector<std::vector<float>>& (note the reference).

you need std::decay/std::remove_reference for your tests, i.e.:

has_size<std::decay_t<decltype(y)>>::value

Jarod42
  • 203,559
  • 14
  • 181
  • 302