2

TCLAP std::wstring issue. Hello All,

I am using TCLAP(command line parsing utility). It has worked well so far until I ran into issues with wstring processing capability.

I have the below line to parse code for an UnlabeledValueArg

std::wstring defaultValue; 

UnlabeledValueArg<std::wstring>
serverName("COMPUTERNAME", "List all scheduled tasks of computer", false,
           defaultValue, "string" );

I run into compile time issues ( Visual Studio spits out incredulous amounts of issues.). My question is have anyone tried using std::wstring with TCLAP. If yes can you post the solution or the approach to it. Thanks.

Edit - The compiler warnings I get is -

C:\DevProjects\MyProjects\nttoolkit\trunk\external\tclap/StandardTraits.h(187) : error C2679: binary '=' : no operator found which takes a right-hand operand of type 'const std::string' (or there is no acceptable conversion)

    c:\Program Files (x86)\Microsoft Visual Studio

9.0\VC\include\xstring(914): could be 'std::basic_string<_Elem,_Traits,_Ax>

&std::basic_string<_Elem,_Traits,_Ax>::operator

=(const std::basic_string<_Elem,_Traits,_Ax> &)' with [ _Elem=wchar_t, _Traits=std::char_traits, _Ax=std::allocator ]

    c:\Program Files (x86)\Microsoft Visual Studio

9.0\VC\include\xstring(919): or 'std::basic_string<_Elem,_Traits,_Ax> &std::basic_string<_Elem,_Traits,_Ax>::operator =(const _Elem *)' with [ _Elem=wchar_t, _Traits=std::char_traits, _Ax=std::allocator ]

    c:\Program Files (x86)\Microsoft Visual Studio

9.0\VC\include\xstring(924): or 'std::basic_string<_Elem,_Traits,_Ax> &std::basic_string<_Elem,_Traits,_Ax>::operator =(_Elem)' with [ _Elem=wchar_t, _Traits=std::char_traits, _Ax=std::allocator ] while trying to match the argument list '(std::wstring, const std::string)'

    C:\DevProjects\MyProjects\nttoolkit\trunk\external\tclap/Arg.h(446)

: see reference to function template instantiation 'void TCLAP::SetString(T &,const std::string &)' being compiled with [ T=std::wstring ]

    C:\DevProjects\MyProjects\nttoolkit\trunk\external\tclap/ValueArg.h(391)

: see reference to function template instantiation 'void TCLAP::ExtractValue(T &,const std::string &,TCLAP::StringLike)' being compiled with [ T=std::wstring ]

    C:\DevProjects\MyProjects\nttoolkit\trunk\external\tclap/ValueArg.h(389)

: while compiling class template member function 'void TCLAP::ValueArg::_extractValue(const std::string &)' with [ T=std::wstring ]

    C:\DevProjects\MyProjects\nttoolkit\trunk\external\tclap/ValueArg.h(325)

: while compiling class template member function 'bool TCLAP::ValueArg::processArg(int *,std::vector<_Ty> &)' with [ T=std::wstring, _Ty=std::string ]

    C:\DevProjects\MyProjects\nttoolkit\trunk\external\tclap/UnlabeledValueArg.h(44)

: see reference to class template

instantiation 'TCLAP::ValueArg' being compiled with [ T=std::wstring ]

    C:\DevProjects\MyProjects\nttoolkit\trunk\tasksecure\src\main.cpp(26)

: see reference to class template instantiation 'TCLAP::UnlabeledValueArg' being compiled with [ T=std::wstring ]

TaskSecure - 1 error(s), 6 warning(s)

2
  • What are the errors VS is giving you? Commented May 12, 2011 at 16:35
  • I've just edited and added the compiler warnings. Thanks. Commented May 18, 2011 at 14:49

2 Answers 2

2

I downloaded the source and built your example. The UnlabeledValueArg does take std::string, so apologies. The StandardTraits.h:202 shows dst = src. It is trying to set std::string to a std::wstring and there is no conversion. Apparently, TCLAP does not support std::wstring. The src should be std::wstring.

One work around might be to use std::string instead, and UTF-8 encode your program's arguments before using TCLAP.

Another option is to ask the owner of TCLAP to extend it to support either wide and narrow arguments.

Sign up to request clarification or add additional context in comments.

5 Comments

When in doubt kindly add it as a comment. Also try having a look at the method signature before posting replies.
@Eternal Learner - the method signature indicates: no operator found which takes a right-hand operand of type 'const std::string'. To me, that looks like you should be using wide strings.
Maybe having a look at this might give you a better idea. - tclap.sourceforge.net/html/annotated.html . Have a look at the UnlabeledValueArg class constructor.
@Eternal Learner - I downloaded the source and built your example. The UnlabeledValueArg does take std::string, so apologies. The StandardTraits.h:202 shows dst = src. It is trying to set std::string to a std::wstring and there is no conversion. Apparently, TCLAP does not support std::wstring. The src should be std::wstring. A work around might be to use std::string instead, and UTF-8 encode your program's arguments before using TCLAP.
Thanks for your suggestion. Yes I am looking into that now and trying to add support to std::wstring. I don't know much about UTF-8 encoding. Thanks will look into it. Kindly edit your answer so that I can remove my down-vote(thanks for taking time off and undergoing the pain of downloading the source).
1

I also had a problem with TCLAP trying to process Unicode in command lines. I solved it by modifying TCLAP to use wchar_t and std::wstring. So I've made WTCLAP (Wide Templatized Command Line Argument Parser - https://github.com/vasyutin/wtclap). Maybe it will be useful for you, too. You can even use TCLAP and WTCLAP together in platform independent code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.