1

I'm trying to retrofit my program with boost::program_options. Among other things, I've written a function which I just can't get to compile. Here's a sort-of-minimal .cpp file which fails compilation:

#include <boost/program_options.hpp>

template <typename C, typename E>
inline bool contains(const C& container, const E& element) {
    return container.find(element) != container.end();
}

template <typename K, typename V>
V& updateFromConfig(V& updatee, const K& key, const po::variables_map& vm) {
    if (contains(vm, key)) {
        // option 1
        updatee = vm[key];
        // option 2
        //updatee = vm[key].variable_value();
        // option 3
        // updatee = vm[key].as<V>();
        // option 4
        // updatee = vm[key].as();
    }
    return updatee;
}

template size_t& updateFromConfig<char*,size_t>(size_t& updatee, char* const& key, const po::variables_map& vm);

With any of the four options I get a different compiler error:

  • With option 1:

    cannot convert ‘const boost::program_options::variable_value’ to ‘long unsigned int’ in assignment
       updatee = vm[key];
    
  • With option 2:

    invalid use of ‘boost::program_options::variable_value::variable_value’
    
  • With option 3:

    18:25: error: expected primary-expression before ‘>’ token
    18:27: error: expected primary-expression before ‘)’ token
    
  • With option 4:

    no matching function for call to ‘boost::program_options::variable_value::as() const
    

What am I doing wrong?

Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524
einpoklum
  • 118,144
  • 57
  • 340
  • 684

1 Answers1

2

C++ syntax for the win. Do:

updatee = vm[key].template as<V>();
Claudiu
  • 224,032
  • 165
  • 485
  • 680
  • Saying why this template disambiguator is required might help the OP. Or I could just Mjölnir the OP to a description... – Yakk - Adam Nevraumont Nov 20 '15 at 18:41
  • Really? That is so bizarre... how can I put "template" in the middle of a statement? Can you perhaps link to somewhere explaining how this is possible? I know about "typename" before types, but this is new to me. – einpoklum Nov 20 '15 at 18:41
  • @einpoklum See the [duplicate](http://stackoverflow.com/q/610245/1774667) pointer on your question now. It explains in ... lots of detail. – Yakk - Adam Nevraumont Nov 20 '15 at 18:43
  • @Yakk Hm? But `as` isn't a dependent name... `vm` isn't a template argument and `operator[]` isn't a function template either. Why would the `template` keyword matter? – Barry Nov 20 '15 at 19:16
  • @Barry [temp.dep.expr]/1 "Except as described below, an expression is type-dependent if any subexpression is type-dependent." So, `vm[k]` is a dependent expression, unless excluded. None of the exclusions apply (the fact that there is only one possible overload of `operator[]` that can be called is not listed as an exclusion). Once dependent, `vm[k].as` is presumed to be not-a-template unless disambiguated. The exceptions to viral type-dependency are mostly stuff like `static_cast` and `sizeof` and `new` etc. – Yakk - Adam Nevraumont Nov 20 '15 at 19:29