Home > Software engineering >  C 11 How to create template function to convert primitives and std::vector to std::string with mini
C 11 How to create template function to convert primitives and std::vector to std::string with mini

Time:03-24

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 does 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 chooses 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;
                                                                                   ^

CodePudding user response:

You are explicitly selecting the wrong template instantiations for your function calls. If you remove the template parameter lists and let the compiler deduce them automatically, it works:

Compiler explorer

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

template <typename T>
std::string toString(const T& obj)
{
    return std::to_string(obj); // <- no template param list
}

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(obj[i]); // <- no template param list
        if(i!=obj.size()-1)
            ss<<", ";
    }
    ss<<"]";
    return ss.str();
}

template<typename ResultType>
class Foo
{
public:
    void PrintResult()
    {
        std::cout << toString(result)<<std::endl; // <- no template param list
    }
    virtual void Do(std::vector<void*> args)=0;
    virtual ~Foo(){}
protected:
    ResultType result;
};


int main()
{
    int intValue = 42;
    std::vector<int> vectorOfInts;
    std::vector<std::vector<double>> vectorOfVectorsOfDoubles;
    std::cout<<toString<int>(intValue)<<std::endl; // <- no template param list
    std::cout<<toString(vectorOfInts)<<std::endl; // <- no template param list
    std::cout<<toString(vectorOfVectorsOfDoubles)<<std::endl; // <- no template param list
    return EXIT_SUCCESS;
}
  • Related