Template-based data reading from string

Hi all,

I am trying to implement a class to store data, which should be able to read its data from a string (or stringstream).

I am trying something like

1
2
3
Type value;              // Type is the template type
wstringstream wss = ...  // holds the substring with the data
wss >> value;


For numerical types, this works well. However, when Type is a string type, ">>" stops after the first whitespace. I want to have all the data from wss; for the string types, this would just be a copy. How can I achieve this, template-based?

I tried this, where I put the conversion in a separate function, to create specializations for string data types (only wstring shown here). This compiles fine, but my linker (VS2005) claims that there are duplicate symbols (error LNK2005). Obviously it can't distinguish between the two _convert functions.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// Laden aus XML
template <typename Typ> void Parameter<Typ>::_convert(const wstring& data, Typ& value)
{
	wstringstream result(data);
	result >> value;
}
void Parameter<wstring>::_convert(const wstring& data, wstring& value)
{
	value = data;
}
template <typename Typ> void Parameter<Typ>::XMLLaden(const wstring xml)
{
	// Array aller möglichen XML-Tags bauen
	VectorWString xml_tags = GetXMLZusatztags();
	xml_tags.push_back(GetKurzname());

	for (VectorWString::iterator tag = xml_tags.begin(); tag != xml_tags.end(); tag++)
	{

		// Regex zusammenbauen
		wstring str = L"<" + *tag + L">(.*?)</" + *tag + L">";

		boost::wregex expression(str, boost::regex::icase);
		boost::match_results<wstring::const_iterator> match;

		if (boost::regex_search(xml, match, expression))
		{
			Typ w;
			_convert(wstring(match[1].first, match[1].second), w);

			SetWert(w);
		}
	}
}


Sorry for the mixed language in the source code. I hope you get the idea.

Any ideas are highly appreciated!

Best regards
Matthias
Last edited on
Specialize the function int which you are calling the >>, so you can make it act differently for strings
Hi,
I just edited my original post to show that I tried exactly what you suggested. Did not work as expected...
Last edited on
You need to add template<> before the specialized function
like this? same problem as above

1
2
3
4
template <> void Parameter<wstring>::_convert(const wstring& data, wstring& value)
{
	value = data;
}

I also tried to put the _convert function in global scope etc. The linker always claims that duplicate symbols exist.
Have you properly gated the include file?
Tataaa! I got it.

kbw, you pointed me in the right direction. Yes, I believed I gated the include file correctly (if that means to wrap it in a #ifndef xxx / #define xxx / [..code..] / #endif construct).

Yesterday evening it came to me. The linker wasn't complaining about not being able to distinguish the template _convert function and its specializations, but meant to complain about the specialized function appearing more than once, because I include the corresponding file almost anywhere in the project since the base class relies on it...

So, I have just put a forward declaration of the specialization in the header,

parameter.h:
1
2
3
4
5
6
7
// Laden aus XML
template<typename Typ> void Parameter<Typ>::_convert(const wstring& data, Typ& value)
{
	wstringstream result(data);
	result >> value;
};
template<> void Parameter<wstring>::_convert(const wstring& data, wstring& value);


and put the function definition in a corresponding parameter.cpp,

parameter.cpp:
1
2
3
4
5
6
#include "Parameter.h"

template<> void Parameter<wstring>::_convert(const wstring& data, wstring& value)
{
	value = data;
};


Thanks for all your help and patience!
Matthias
Topic archived. No new replies allowed.