vendredi 26 mars 2021

Compiling error | cannot compile cpp program due to compiler parser

Short Description
I am trying to compile a short lambda to convert a giving input to string format. My overall class uses a template, i.e template <typename T>. I want to overload << to print-out an object which can be of any type; i.e, int, string, etc.

For example, let's say that my object is {int age: 12, int* left: null, int* right: null}, I should be able to print something like "{value: "12", left: undefined, right: undefined}". In addition, if object = {string comment: "Distance learning....", int* left: undefined, int* right: undefined}, I should print out, "{value: "Distance learning....", left: undefined, right: undefined}". Below is a copy of the lambda function mark-up to convert from any data-type to a string.

std::function<std::string(T)> toString; //declares the blueprint for the lambda function, notice that I am returning a string, and expecting an object of type T, which can be of any type since i'm using a template. 
    toString = [](T value) -> std::string { //Begins the the body of the function
      if (typeid(std::string).name() == typeid(T).name())
      { // not the best way to do this, but here I am checking that the input is already a string, in which case I do not need to convert or anything. 
        return value; //if the input 'value' is already a string, simply return it. 
      }
      //do some stuff to convert the date into a string

      return; // return the final 'value' in string format. 
    };

~Sorry in advance if my comments were confusing.

Problem
The idea works on paper, however, the problem happens when I have a data-type that is not of type string. Let's say that T == int, walking through the code, the if statement will be skipped, assuming that my condition is set-up correctly, and I should move down. This means that I will not return an int when the function blueprint says that I will return a string.

However, when the compiler goes through my code, it reads it and thinks that I am trying to send-back an int when the function is supposed to send back a string and it throws an error. "no viable conversion from returned value of type 'int' to function return type 'std::string'"

for example,

//Let T = int, value = 12, so the below parameter equals, int value, which holds the value of 12. 

    toString = [](T value) -> std::string {
      if (typeid(std::string).name() == typeid(T).name())
      { //should failed since typeid(int).name != typeid(std::string).name
        return value;
      }
      //do some stuff to convert the date into a string

      return; // return the final 'value' in string format. 
    };
//However, when the compiler parses the code, it thinks that I am trying to return value, which is of type int. But the function is supposed to return a string.

//In reality, this should not happen since my if statement will take care of it. 

Does anyone know a work around for this or any idea on how to fix my logic?

Aucun commentaire:

Enregistrer un commentaire