K
Krivenok Dmitry
I writing simple class CmdLine:
.....
.....
class CmdLine
{
.....
.....
public:
/// Constructor
CmdLine(int argc, char** argv);
/// Destructor
~CmdLine();
template <class T>
void GetOption (const std::string& OptionName, T& OptionVar) const
{
if ( Loki::TypeTraits<T>::isStdIntegral )
{
std::cout << "bool\n"; //For debug
OptionVar = BoolOptions_.find(OptionName)->second;
}
else
{
std::cout << "string\n"; //for debug
OptionVar = StringOptions_.find(OptionName)->second;
}
}
private:
/// Map for string options, e.g. Script File Name
std::map<std::string,std::string> StringOptions_;
/// Map for bool options, e.g. Disable StdErr Output
std::map<std::string,bool> BoolOptions_;
....
....
};
.....
.....
I use TypeTraits from Loki Library by A. Alexandrescu to determine
types class (integral or not).
If type is integral (e.g. bool), then I use BoolOptions_ map. Else
I use StringOptions_ map.
My main function :
#include <string>
#include <iostream>
#include "Cmdline.hpp"
#include "Cfg.h"
using namespace std;
int main( int argc, char* argv[] )
{
CmdLine CC(argc,argv);
bool b;
std::string s;
CC.GetOption("str_opt",s);
CC.GetOption("bool_opt",b);
return 0;
}
But my code doesn't compile!
Errors are :
CmdLineParse/Cmdline.hpp: In member function `void
CmdLine::GetOption(const
std::string&, T&) const [with T = bool]':
Application/Application.cpp:20: instantiated from here
CmdLineParse/Cmdline.hpp:52: cannot convert `const std::string' to
`bool' in
assignment
But if I comment assigments (OptionVar = ...), then my code compile
succ
and output is:
string
bool
In this case used valid branch of 'if' statment.
Why my code doesn't compile if first case???
.....
.....
class CmdLine
{
.....
.....
public:
/// Constructor
CmdLine(int argc, char** argv);
/// Destructor
~CmdLine();
template <class T>
void GetOption (const std::string& OptionName, T& OptionVar) const
{
if ( Loki::TypeTraits<T>::isStdIntegral )
{
std::cout << "bool\n"; //For debug
OptionVar = BoolOptions_.find(OptionName)->second;
}
else
{
std::cout << "string\n"; //for debug
OptionVar = StringOptions_.find(OptionName)->second;
}
}
private:
/// Map for string options, e.g. Script File Name
std::map<std::string,std::string> StringOptions_;
/// Map for bool options, e.g. Disable StdErr Output
std::map<std::string,bool> BoolOptions_;
....
....
};
.....
.....
I use TypeTraits from Loki Library by A. Alexandrescu to determine
types class (integral or not).
If type is integral (e.g. bool), then I use BoolOptions_ map. Else
I use StringOptions_ map.
My main function :
#include <string>
#include <iostream>
#include "Cmdline.hpp"
#include "Cfg.h"
using namespace std;
int main( int argc, char* argv[] )
{
CmdLine CC(argc,argv);
bool b;
std::string s;
CC.GetOption("str_opt",s);
CC.GetOption("bool_opt",b);
return 0;
}
But my code doesn't compile!
Errors are :
CmdLineParse/Cmdline.hpp: In member function `void
CmdLine::GetOption(const
std::string&, T&) const [with T = bool]':
Application/Application.cpp:20: instantiated from here
CmdLineParse/Cmdline.hpp:52: cannot convert `const std::string' to
`bool' in
assignment
But if I comment assigments (OptionVar = ...), then my code compile
succ
and output is:
string
bool
In this case used valid branch of 'if' statment.
Why my code doesn't compile if first case???