Hi,
I have a header file where I store all the typedefs.
One of these typedefs uses a constant that I need to initialize in another .cpp. The code that I have tried is this:
1 2 3 4
// ConfigurationFile.hpp
// more typedefs.
externconstunsignedint number;
typedef std::map<std::string,number> MyMap;
1 2 3 4 5 6 7 8 9
// Initializer.hpp
class Initializer{
private:
// more things
public:
// more things
void Execute();
};
1 2 3 4 5 6 7 8 9 10
// Initializer.cpp
#include Initializer.hpp
#include ConfigurationFile.hpp
void Initializer::Execute() {
// more things
constunsignedint number( ReadNumberFromConfFile() );
// more things
}
ReadNumberFromConfFile() reads the value from a configuration file that always exists.
This code doesn't compile. I get this error message:
1 2 3
error: ‘number’ is not a valid template argument for type ‘unsignedint’ because it is a non-constant expression
error: invalid type in declaration before ‘;’ token
So, is it possible to create a constant inside a method and pass it to the header file for the typedef?.
In the type-definition of MyMap, you must specify a type for both template parameters. If you're using C++11, you can use decltype() to obtain the type of its operand. For instance:
Templates consist of template parameters. These parameters normally require a type given to them, such as int, and float. When you pass a variable to a template parameter list when it expects a type, the compiler will evaluate the argument, which will be a value, not a type. Therefore, the compiler will not be able to deduce the type from the resulting value. For instance:
#1) This is OK, because the template parameter T of S is given an actual type.
#2) This is not OK, because v_ evaluates to zero, which is not a type, but a integral constant.
Ok. I'm using now g++ -std=c++0x and get this complaint:
1 2 3
error: type/value mismatch at argument 2 in template parameter list for ‘template<class T, unsignedint k> struct MyMap’
error: expected a constant of type ‘unsignedint’, got ‘constunsignedint’
Nope! I'm getting the errors I said in my previous post!
1 2 3
error: type/value mismatch at argument 2 in template parameter list for ‘template<class T, unsignedint k> struct MyMap’
error: expected a constant of type ‘unsignedint’, got ‘constunsignedint’
OK, the only way I can see for myself is by seeing the actual code. Could you please use this website: http://pastebin.com/ and post the links to your code.
error: type/value mismatch at argument 2 in template parameter list for ‘template<class T, int k> class TemplatedClass’
error:expected a constant of type ‘int’, got ‘constint’
I'm compiling with: g++ -std=c++0x main.cpp
If I don't use decltype(), everything works.
PS: My version of g++ is 4.4.3 and the S.O. is Ubuntu 10.04.
TemplatedClass expects an integral value for its second template parameter, not a type, because the compiler knows its a constant integer type. Therefore, decltype() will not work because it yields a type, not a value. You have to pass it an integral value.