mercredi 23 mars 2022

C++11 How to create template function to convert primitives and std::vector to std::string with minimum specializations?

The point where I stuck is described in the title.

But, maybe, there are another solution of my global problem.

There are template base class, which do some work, and I need to print results for debugging purposes.


template<typename ResultType>

class Foo

{

public:

    void PrintResult()

    {

        std::cout<<toString<ResultType>(result)<<std::endl;

    }

    virtual void Do(std::vector<void*> args)=0;

    virtual ~Foo(){}

protected:

    ResultType result;

};

I need mostly to print primitives, std::string, and std::vector of primitives, strings or vectors.

I decided to use templates for this, but I can not specialize template for std::vector. I don't know why it choose wrong overload.


#include <iostream>

#include <sstream>

#include <vector>

template <typename T>

std::string toString(const T& obj)

{

    return std::to_string(obj);

}

template <typename T>

std::string toString(const std::vector<T>& obj)

{

    std::stringstream ss;

    ss<<"[";

    for(size_t i=0;i<obj.size();++i)

    {

        ss<<toString<T>(obj[i]);

        if(i!=obj.size()-1)

            ss<<", ";

    }

    ss<<"]";

    return ss.str();

}

int main()

{

    int intValue = 42;

    std::vector<int> vectorOfInts;

    std::vector<std::vector<double>> vectorOfVectorsOfDoubles;

    std::cout<<toString<int>(intValue)<<std::endl;

    std::cout<<toString<std::vector<int>>(vectorOfInts)<<std::endl;

    std::cout<<toString<std::vector<std::vector<double>>>(vectorOfVectorsOfDoubles)<<std::endl;

    return EXIT_SUCCESS;

}

Errors:


./main.cpp:-1: In instantiation of ‘std::__cxx11::string toString(const T&) [with T = std::vector<int>; std::__cxx11::string = std::__cxx11::basic_string<char>]’:

./main.cpp:33: required from here

./main.cpp:8: error: no matching function for call to ‘to_string(const std::vector<int>&)’

     return std::to_string(obj);

            ~~~~~~~~~~~~~~^~~~~

./main.cpp:-1: In instantiation of ‘std::__cxx11::string toString(const T&) [with T = std::vector<std::vector<double> >; std::__cxx11::string = std::__cxx11::basic_string<char>]’:

./main.cpp:34: required from here

./main.cpp:8: error: no matching function for call to ‘to_string(const std::vector<std::vector<double> >&)’

     return std::to_string(obj);

            ~~~~~~~~~~~~~~^~~~~

Compiler:

GCC 4.8 (C++, x86 64bit)

Is there any possibilities not to specialize template function with all primitives and vectors?

I tried adapt solution from C++ How to specialize a template using vector<T>? with fix for c++11 from https://ru.stackoverflow.com/questions/500735/stdenable-if-t

But I still get errors:

#include <iostream>
#include <sstream>
#include <vector>
#include <type_traits>

template<bool C, class T = void>
using enable_if_t = typename std::enable_if<C, T>::type;

template<typename T>
struct is_vector
{
    static constexpr bool value = false;
};

template<template<typename...> class C, typename U>
struct is_vector<C<U>>
{
    static constexpr bool value =
        std::is_same<C<U>,std::vector<U>>::value;
};

template <typename T>
enable_if_t<!is_vector<T>::value,std::string> toString(T& obj)
{
    return std::to_string(obj);
}

template <typename T>
enable_if_t<is_vector<T>::value,std::string> toString(std::vector<T>& obj)
{
    std::stringstream ss;
    ss<<"[";
    for(size_t i=0;i<obj.size();++i)
    {
        ss<<toString<T>(obj[i]);
        if(i!=obj.size()-1)
            ss<<", ";
    }
    ss<<"]";
    return ss.str();
}

int main()
{
    int intValue = 42;
    std::vector<int> vectorOfInts;
    std::vector<std::vector<double>> vectorOfVectorsOfDoubles;

    std::cout<<toString<int>(intValue)<<std::endl;
    std::cout<<toString<std::vector<int>>(vectorOfInts)<<std::endl;
    std::cout<<toString<std::vector<std::vector<double>>>(vectorOfVectorsOfDoubles)<<std::endl;
    return EXIT_SUCCESS;
}
./main.cpp:-1: In function ‘int main()’:
./main.cpp:50: error: no matching function for call to ‘toString<std::vector<int, std::allocator<int> > >(std::vector<int>&)’
     std::cout<<toString<std::vector<int>>(vectorOfInts)<<std::endl;
                                                       ^
./main.cpp:23: candidate: template<class T> enable_if_t<(! is_vector<T>::value), std::__cxx11::basic_string<char> > toString(T&)
 enable_if_t<!is_vector<T>::value,std::string> toString(T& obj)
                                               ^~~~~~~~
./main.cpp:23: note:   template argument deduction/substitution failed:
./main.cpp:29: candidate: template<class T> enable_if_t<is_vector<T>::value, std::__cxx11::basic_string<char> > toString(std::vector<T>&)
 enable_if_t<is_vector<T>::value,std::string> toString(std::vector<T>& obj)
                                              ^~~~~~~~
./main.cpp:29: note:   template argument deduction/substitution failed:
./main.cpp:50: note:   cannot convert ‘vectorOfInts’ (type ‘std::vector<int>’) to type ‘std::vector<std::vector<int>, std::allocator<std::vector<int> > >&’
     std::cout<<toString<std::vector<int>>(vectorOfInts)<<std::endl;
                                                       ^
./main.cpp:51: error: no matching function for call to ‘toString<std::vector<std::vector<double, std::allocator<double> >, std::allocator<std::vector<double, std::allocator<double> > > > >(std::vector<std::vector<double> >&)’
     std::cout<<toString<std::vector<std::vector<double>>>(vectorOfVectorsOfDoubles)<<std::endl;
                                                                                   ^
./main.cpp:23: candidate: template<class T> enable_if_t<(! is_vector<T>::value), std::__cxx11::basic_string<char> > toString(T&)
 enable_if_t<!is_vector<T>::value,std::string> toString(T& obj)
                                               ^~~~~~~~
./main.cpp:23: note:   template argument deduction/substitution failed:
./main.cpp:29: candidate: template<class T> enable_if_t<is_vector<T>::value, std::__cxx11::basic_string<char> > toString(std::vector<T>&)
 enable_if_t<is_vector<T>::value,std::string> toString(std::vector<T>& obj)
                                              ^~~~~~~~
./main.cpp:29: note:   template argument deduction/substitution failed:
./main.cpp:51: note:   cannot convert ‘vectorOfVectorsOfDoubles’ (type ‘std::vector<std::vector<double> >’) to type ‘std::vector<std::vector<std::vector<double> >, std::allocator<std::vector<std::vector<double> > > >&’
     std::cout<<toString<std::vector<std::vector<double>>>(vectorOfVectorsOfDoubles)<<std::endl;
                                                                                   ^

Aucun commentaire:

Enregistrer un commentaire