0

Is there a unique way to achieve a uniform call syntax for a convert function like the following for any kind of type? The function takes a string and converts it into the given TYPE (here int and MyMatrix<double>::Vector3 , call by reference of course!!)

int a;
std::string b = "asd";

stringToType::call(a,b);

MyMatrix<double>::Vector3 g; // Vector3 might be any type e.g  Eigen::Matrix<double,3,1>
stringToType::call(g,b);

e.g:

template<typename T>
struct MyMatrix{
    typedef Eigen::Matrix<T,3,1> Vector3;
};

I would like that the convert function converts types in the form of Eigen::Matrix<T,3,1> with T arbitary with the same function,

It should also support the basic types which have no template parameters (like the int)

Gabriel
  • 8,990
  • 6
  • 57
  • 101
  • the ``call`` function should do some convertion (not important) from the given string into the type given – Gabriel Sep 13 '13 at 15:05
  • Pretend for a moment you're someone that *doesn't* know what *you* are thinking when reading what you *wrote* here. The state your *problem* in a way that is understandable. (And no partial specialization doesn't mean you can't *overload*, which is what you should be doing). – WhozCraig Sep 13 '13 at 15:05
  • thanks for the fast help but please reread it should be clearer now – Gabriel Sep 13 '13 at 15:12
  • It is much better to use C++11 style using declaration, as `template using Vector3 = Eigen::Matrix;` so you can use `Vector3 g;` Looks much clean! – Nawaz Sep 13 '13 at 15:17
  • Yes of course thanks!, right now I am not yet using these c+11 features... – Gabriel Sep 13 '13 at 15:18

1 Answers1

1

You may want something like that:

#include <string>
#include <sstream>

namespace details
{
    template <typename T>
    struct stringToTypeImpl
    {
        void operator () (std::stringstream& ss, T& t) const
        {
            ss >> t;
        }
    };

    // And some specializations
    template <typename T, int W, int H>
    struct stringToTypeImpl<Eigen::Matrix<T, W, H> >
    {
        void operator () (std::stringstream& ss, Eigen::Matrix<T, W, H>& t) const
        {
            for (int j = 0; j != H; ++j) {
                for (int i = 0; i != W; ++i) {
                    stringToTypeImpl<T>()(ss, t(i, j)); //ss >> t(i, j);
                }
            }
        }
    }
    // ...
}

template <typename T>
void stringToType(const std::string& s, T& t)
{
    std::stringstream ss(s);

    details::stringToTypeImpl<T>()(ss, t);
}


int main() {
    std::string s = "42";
    int i;

    stringToType(s, i);
    return 0;
}
Jarod42
  • 203,559
  • 14
  • 181
  • 302
  • jeah, thats it , the important part i was missing was the redirection to details::stringToTypeImpl and the fact that i can add template parameters while i specialize it, i wasnt aware of this ! thanks a lot – Gabriel Sep 15 '13 at 16:10
  • the follow up question is the following: http://stackoverflow.com/questions/18823564/partial-specialzation-with-dependent-name-typename – Gabriel Sep 16 '13 at 08:33