I am completely out of ideas why the following example template will fail to compile on C++11 (gcc 4.9.2). For some reason, compiler requires me to call string()
on already a string
object that is being passed as variables_map.at()
parameter. Note, that variables_map.count()
takes the same argument in it's "pure" form with no objections.
One more observation: if the function is not a template but declared for specific type T, it compiles fine with no errors.
Here's the sample code:
#include <boost/program_options.hpp>
#include <iostream>
#include <map>
#include <utility>
#include <string>
namespace po = boost::program_options;
template <typename T> void test_error(T par, po::variables_map vm) {
std::multimap<T, std::string> mm;
mm.emplace(std::make_pair(par, "test"));
auto a_range = mm.equal_range(par);
for (auto it = a_range.first; it != a_range.second; ++it) {
std::cout << vm.count(std::string(it->second)) << std::endl; //OK
std::cout << vm.count( (it->second)) << std::endl; //OK
std::cout << vm.at(std::string(it->second)).as<T>() << std::endl; //OK
std::cout << vm.at( it->second ).as<T>() << std::endl; //COMPILE ERROR
}
}
And compiler errors:
error: expected primary-expression before ‘>’ token
std::cout << vm.at( it->second) .as<T>() << std::endl; //COMPILE ERROR
^
error: expected primary-expression before ‘)’ token
std::cout << vm.at( it->second) .as<T>() << std::endl; //COMPILE ERROR
^