Can you statically state that an object can be converted to a specific type?

I am currently working on a console graphical library for C ++ 11 to facilitate some debugging and more.

For a specific class that contains templates, I want to make sure that I can convert the template type to a string before printing it.

Example:

template<typename T>
class listbox {
private:
    std::vector<T> list;
    [...]

public:
    std::string print_item(T& item) { /* static_assert() here */}
}

So, in the "Static Assert" section, I want to check if I can convert the element to std::string(or const char*will work the same way), so really the question is simple, how can I approve the conversion from a template type?

I know that the / ide compiler will respond to types that cannot recognize it, but I need a fixed type to be able to control the string more.

+4
1

, ! std::is_convertible

template<typename T>
class listbox {
private:
    std::vector<T> list;
    [...]

public:
    std::string print_item(T& item) {
      static_assert(std::is_convertible<T, const char*>::value, "Not stingifyable");
      // More work
    }
}
+6

Source: https://habr.com/ru/post/1624274/


All Articles