mercredi 26 juin 2019

Why does the function return two different values even when nothing is changed in the function call?

In the first case the code returns -1

#include <iostream>

template <typename T>
int compare( const T &val1, const T &val2){

    if(val1 < val2) return -1;
    if(val2 > val1) return 1;
    return 0;
}

int main(){

    std::string v1= "hello", v2 = "world";
    std::cout << compare("hello", "world") << std::endl;
}

In the second case the code returns zero even when there is no change in the method call.

#include <iostream>

template <typename T>
int compare( const T &val1, const T &val2){

    if(val1 < val2) return -1;
    if(val2 > val1) return 1;
    return 0;
}

int main(){

    std::cout << compare("hello", "world") << std::endl;
}

I am using g++ 7.4.0.

Aucun commentaire:

Enregistrer un commentaire