dimanche 19 juin 2016

implicit operator bool() is not responding to my call

I am testing a template :

template<typename Key>
struct _Tarray<Key, char>{

    map<const Key, string> data; 

    //stuff

    operator bool(){cout << " just testing"; return false;}    //trying to call this, but its not answering
}; 

so, i am driving it like this;

_Tarray<int, char> out;
  cout << out; 

Here is my overloaded operator<<;

template<typename T, typename U>       
ostream& operator<< (ostream& os, const _Tarray<T, U>& var){

    //now i expect operator bool() to be called here

    if(var){os << " Yes !";}         //and bool is not being called. my compiler (g++) is angry
    else{cout << " No !";}

    return os;
}

Question: I need an explanation as to, why operator bool() is not being called. Or am I doing anything wrongly?

Error:

./template.h: In instantiation of 'std::ostream& operator<<(std::ostream&, const _Tarray<T, U>&) [with T = int; U = char; std::ostream = std::basic_ostream<char>]':
./template.h:128:41:   required from 'void var_dump(const T&) [with T = _Tarray<int, char>]'
functions.cpp:12:21:   required from here
./template.h:122:5: error: passing 'const _Tarray<int, char>' as 'this' argument discards qualifiers [-fpermissive]
     if(var){os << "heheheheh";}
     ^
./template.h:73:5: note:   in call to '_Tarray<Key, char>::operator bool() [with Key = int]'

Aucun commentaire:

Enregistrer un commentaire