Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

What is wrong with my template gJoin() function?

41 views
Skip to first unread message

Szyk Cech

unread,
Jul 6, 2019, 11:54:28 AM7/6/19
to
Hi
I want to write nice join template function. In order to work with
different containers and with wide and narrow strings it must be template.
Now I have some thing like this:


/**
* @brief Joins strings in container.
* @param aStringList an container wiht strings.
* @param aDelimiter string witch joins strings in container.
* @result String joined with delimiter.
*/

template<template<typename> typename tContainer, typename tString>
tString gJoin(const tContainer<tString>& aStringList, const tString&
aDelimiter)
{
tString lResult;
for(tString lPart : aStringList)
{
if(lResult.size() > 0)
lResult += aDelimiter;
lResult += lPart;
}

return lResult;
}

But it not works. My g++ is:
gcc version 7.4.0 (Ubuntu 7.4.0-1ubuntu1~18.04.1)

I learn that it should work from:
https://en.cppreference.com/w/cpp/language/template_parameters

Error is like this:

/home/szyk/!-EnergoKod/!-Libs/EnergoKodTools2/Tests/Src/StringTest.cpp:
In member function ‘void StringTest::test7()’:
/home/szyk/!-EnergoKod/!-Libs/EnergoKodTools2/Tests/Src/StringTest.cpp:126:76:
error: no matching function for call to
‘gJoin<std::vector<std::__cxx11::basic_string<char,
std::char_traits<char>, std::allocator<char> >,
std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>,
std::allocator<char> > > >,
std::__cxx11::string>(std::vector<std::__cxx11::basic_string<char> >&,
std::__cxx11::basic_string<char>)’
std::string lTest1A(gJoin<vector<string>, string>(lVector1,
string(" ")));

^
In file included from
/home/szyk/!-EnergoKod/!-Libs/EnergoKodTools2/Tests/Src/StringTest.cpp:4:0:
/home/szyk/!-EnergoKod/!-Libs/EnergoKodTools2/Include/EnergoKodTools2/Strings.h:148:9:
note: candidate: template<template<class> class tContainer, class
tString> tString ekt2::gJoin(const tContainer<tString>&, const tString&)
tString gJoin(const tContainer<tString>& aStringList, const tString&
aDelimiter)
^~~~~
/home/szyk/!-EnergoKod/!-Libs/EnergoKodTools2/Include/EnergoKodTools2/Strings.h:148:9:
note: template argument deduction/substitution failed:
Tests/CMakeFiles/EnergoKodTools2Test.dir/build.make:158: recipe for
target 'Tests/CMakeFiles/EnergoKodTools2Test.dir/Src/StringTest.cpp.o'
failed
make[2]: ***
[Tests/CMakeFiles/EnergoKodTools2Test.dir/Src/StringTest.cpp.o] Error 1
make[1]: *** [Tests/CMakeFiles/EnergoKodTools2Test.dir/all] Error 2
make: *** [all] Error 2
CMakeFiles/Makefile2:122: recipe for target
'Tests/CMakeFiles/EnergoKodTools2Test.dir/all' failed
Makefile:129: recipe for target 'all' failed
17:45:51: Proces "/usr/bin/cmake" zakończył się kodem wyjściowym 2.
Błąd budowania / instalowania projektu EnergoKodTools2 (zestaw narzędzi:
Desktop Qt 5.12.3 GCC 64bit2)
Podczas wykonywania kroku "Wersja CMake"


please help me,
best regards,
and thanks in advance
Szyk Cech

Szyk Cech

unread,
Jul 6, 2019, 2:00:02 PM7/6/19
to
Thanks a lot!!!

I improve my code with your help! And it pass all my tests now.

Problem was that I rely on Qt Creator buildin C++ parser (based on CLang
compiler libraries). And how ever I have compile able and valid template
function code, I try crazy calls of this function, because I believe in
parser. Apparently it does not recognize C++17 fully. Not sure, but
maybe it is not upgraded to newest version (but I have newest Qt Creator).


Final version of my gJoin() template function looks like:

/**
* @brief Joins wide and narrow strings in any container.
* @param aStringList an container with strings.
* @param aDelimiter string witch joins strings in container.
* @result String joined with delimiter.
* @warning Don't call this function with different string types
* as containter template parameter and as delimiter - if you do this
you will
* get compile error.
*/

template<template<typename> typename tContainer, typename tString>
tString gJoin(const tContainer<tString>& aStringList, const tString&
aDelimiter)
{
tString lResult;

for(tString lPart : aStringList)
{
if(not lResult.empty())

Heinz Müller

unread,
Jul 14, 2019, 5:16:38 AM7/14/19
to
Your code is wonderful! Everyone should code like that!

Christian Gollwitzer

unread,
Jul 15, 2019, 1:41:43 AM7/15/19
to
Am 14.07.19 um 11:16 schrieb Heinz Müller:
> Your code is wonderful! Everyone should code like that!

ROFL

G G

unread,
Jul 15, 2019, 9:23:01 AM7/15/19
to
-----------------------------


template < typename tContainer, typename tString >
tString gJoin( const tContainer &aStringList, const tString &aDelimiter )
0 new messages