0

I'm trying to parse the command line arguments using variadic templates and tuples. The syntax I'm trying to achieve is

 std::tie(power, nLevels) = ParseCommandLineEx(argc, argv,"Power",3.0,"num levels",4)

That is, I forward main()'s argc & argv arguments, then come a series of argument names alternating with matching typed default arguments. The program should read the arguments (power, nLevels) from the command line, and in case there aren't enough arguments, set the values to the default. Argument names are given for documentation and error messages generation.

The simplified code goes as following (ignoring argument names and errors for now)

 template <typename ARGNAME, typename ARG>
 tuple<ARG> ParseCommandLineEx(int argc,  _TCHAR* argv[], ARGNAME argName, ARG defaultArg)
 {
     if (argc<=0)
         return make_tuple(defaultArg);
    if (argc==1)
         return make_tuple(WideStringToVal<ARG> (argv[0]));

 }

 template <typename ARGNAME, typename ARG, typename ...ARGS>
 auto ParseCommandLineEx(int argc,  _TCHAR* argv[], ARGNAME argName, ARG defaultArg, ARGS... defaultArgs) -> decltype(tuple_cat(make_tuple(defaultArg),      ParseCommandLine<ARGS...>(argc-1,argv+1, defaultArgs...)))
 {
     ARG param = argc>0 ? WideStringToVal<ARG> (argv[0]) : defaultArg;
     return tuple_cat(make_tuple(param), ParseCommandLineEx<ARGS...>(argc-1,argv+1, defaultArgs...));
 }

 template <typename T> 
 T WideStringToVal(TCHAR *str)
 {
     static_assert(false);
 }


 template <> 
 int WideStringToVal(TCHAR *str)
 {
     return _wtoi(str);
 }

 template <> 
 float WideStringToVal(TCHAR *str)
 {
     return float(_wtof(str));
 }

 int _tmain(int argc, _TCHAR* argv[])
 {
   auto params = ParseCommandLineEx(argc-1, argv+1, "v1",-1.0f,"v2",-1,"v3",-1);
   return 0;
 }

But this does not compile, VC++ Nov2012 CTP compiler fails due to "expects 4 arguments - 8 provided" on the line calling ParseCommandLineEx from _tmain. Can anyone spot the problem?

Ophir Gvirtzer
  • 604
  • 4
  • 8
  • 2
    The problem is you are not using Boost.ProgramOptions :) – Lightness Races in Orbit Jun 28 '15 at 10:41
  • That would kill all the fun... – Ophir Gvirtzer Jun 28 '15 at 10:42
  • It seems like it is using the case with 4 arguments. Have you tried explicitly writing which template instance of ParseCommandLineEx that should be called? – Jens Munk Jun 28 '15 at 10:43
  • @Jens Munk -in that case I get " Failed to specialize function template 'unknown-type AlgTester::ParseCommandLineEx(int,_TCHAR *[],ARGNAME,ARG,ARGS...)'" – Ophir Gvirtzer Jun 28 '15 at 10:52
  • 1
    I must say it's annoying to look at it. The code is full of `_TCHAR`s, and `TCHAR`s, and other stuff that's unrelated to the question, really. Having gotten rid of this, the compiler complains that ` error: ‘ParseCommandLine’ was not declared in this scope`, which is true - it hasn't been declared anywhere in the question. – Ami Tavory Jun 28 '15 at 10:53
  • An obvious problem is http://stackoverflow.com/questions/26274207/gcc-can-compile-a-variadic-template-while-clang-cannot. Given that this is an ancient alpha-quality MSVC though, I don't know if that's all of the problem. – T.C. Jun 28 '15 at 10:57
  • Ami, I agree that _TCHAR is annoying, but I wanted the example to be simple and compiling on my machine. – Ophir Gvirtzer Jun 28 '15 at 11:01
  • T.C. - I've tried converting the function to static member function, that didn't help. That ancient 2012 compiler is the best I got... – Ophir Gvirtzer Jun 28 '15 at 12:57
  • @Ophir Gvirtzer Question: Is variadic templates fully supported in VS2012. Have you tried another compiler? – Jens Munk Jun 28 '15 at 14:03
  • @JensMunk Variadic functions are supported in the "November 2012 CTP" compiler version, and they work for me many simpler scenarios. As Ami mentions, it is not an official version. However, I still don't know, whether the problem is in my code or in the compiler. Unfortunately I don't have a newer compiler available. – Ophir Gvirtzer Jun 28 '15 at 14:58
  • Declare `struct magic{};` Then add `template – Yakk - Adam Nevraumont Jun 28 '15 at 15:22
  • @Yakk, thanks, this seems like a great idea based on deep knowledge in template meta programming. For some reason I couldn't make it work, and I switched to another approach. – Ophir Gvirtzer Jun 29 '15 at 16:05

0 Answers0