How can I detect if a type can be streamed to an std::ostream?

前端 未结 6 1581
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-13 00:41

I\'m trying to write a type trait to detect if a type has overloaded operator<<() suitable to use to an output stream.

I\'m missing something because I\'m alwa

6条回答
  •  感动是毒
    2020-12-13 00:55

    EDIT : As found by @jrok, it exist a generic operator<< for rvalue streams that interact badly.

    Something is really wrong here, if you look at the code below tested on coliru, the 2 last lines compile even if it they should not…

    std::stringstream ss;
    B b;
    int v;
    
    std::cout << typeid(decltype(ss>>v )).name() << "\n" ;
    std::cout << typeid(decltype(ss<<1 )).name() << "\n" ;
    std::cout << typeid(decltype(std::declval()>>v )).name() << "\n" ;
    std::cout << typeid(decltype(std::declval()<<1 )).name() << "\n" ;
    
    //std::cout << typeid(decltype(ss>>b )).name() << "\n" ; // do not compile
    //std::cout << typeid(decltype(ss<()>>b )).name() << "\n" ; // should not compile but succeed
    std::cout << typeid(decltype(std::declval()<

提交回复
热议问题