What does it mean when we use a variable like a Template in C++? -


i reading c++ book. encountered code similar following:

int foo=3; if(foo<1>(3))     cout<<"hello world!"<<endl; 

so, how int foo being used template? mean?

i believe : huge ambiguity of c++ because if have template code this, happen?? how c++ handle ambiguity?

template <int n>   void foo( const int t )   {      // ....   } 

this absolutely bizarre code, if book doesn't explain it, burn book , demand refund. summary is: it's not template. it's less comparison, , greater comparison.

if(foo<1>(3)) 

is identical to

if( (foo<1) >3) 

which identical

bool first = (foo < 1);  //false since `foo` 3 bool second = (first > 3); //false evaluates zero, false if (second) //this never entered     cout<<"hello world!"<<endl; //compiler doesn't generate this. 


question template ambiguities, language specifies somewhere default is, though isn't 1 want. compiled sample , found emits this: warning: comparisons 'x<=y<=z' not have mathematical meaning [-wparentheses] http://coliru.stacked-crooked.com/a/49479996464507dc. know it's still being interpreted operators , ignoring templates case.

but yes, there many places c++ "ambiguous" in these ways. common "most vexing parse".

struct {}; struct b {     b(a a) {} }; int main() {     b obj(a()); 

you'd expect create b named obj using default-constructed a, instead declares function named obj returns b , it's parameter itself function takes nothing , returns a. when attempt use variable, sorts of strange , confusing errors: http://coliru.stacked-crooked.com/a/c6fd627be8529b26

a more insidious version this:

template <class t> struct {     static int v; }; template<> struct a<int>     using v = float; }; template<class t> struct b {     b() {         a<t>::v;     } }; 

inside b, it's hard if not impossible tell if v type or variable. 1 bad c++ had add special keyword typename programmers tell compiler type, because compiler assume variable.


Comments

Popular posts from this blog

shopping cart - Page redirect not working PHP -

php - How to modify a menu to show sub-menus -

python - Installing PyDev in eclipse is failed -