mercredi 6 avril 2016

C++11 Call constructor of type after std::is_same to confirm its type

The code below doesn't compile, it complains about "can't convert string to int" when I call func with type Foo and "can't convert int to string" when I call func with type Bar. I thought I already used std::is_same to tell if the type is a Foo or Bar, why this seems to be not working? What would be a better way to do this?

class Foo {
  Foo(int foo){}
};

class Bar {
  Bar(string foo){}
};

template<typename T>
void func(){
  if(std::is_same<T, Foo>::value) {
    T t(1);
  } else {
    T t("aaa");
  }
}

func<Foo>();
func<Bar>();

Aucun commentaire:

Enregistrer un commentaire