-
October 23rd, 2013, 09:08 PM
#1
template class and string default value
Hi,
I would like to define a templated class while implementing default value on templated arguments. I don't know how to do that with string templated variables.
For exemple:
Code:
template <class T>
class A {
public:
A() { version = ???? }
std::string_base<T> version;
};
I don't want to pass the default value as parameter of the constructor.
Does someone know how I can do this?
Thank you,
madric
-
October 24th, 2013, 01:45 AM
#2
Re: template class and string default value
Originally Posted by madric
I would like to define a templated class while implementing default value on templated arguments. I don't know how to do that with string templated variables.
Do you want a default type for the template argument or a default value for your member variable?
The first goes like this
Code:
template <class T = int>
class MyClass
{
//...
};
MyClass<> def;
In the second case, it all depends on what type you require the template argument to be and what kind of default value you want to assign. If it's up to the user of the class, you can consider adding a function object template argument or using a traits class.
(I did not compile this code).
Code:
// using predicate
template <class T, class Initializer>
struct Foo
{
Foo(const Initializer& init = Initializer()) : m(init()) {}
T m;
};
// using traits
template <class T>
struct BarTraits<T>
{
static T init() { return 0; }
};
template <class T, class Traits = BarTraits<T> >
struct Bar
{
Bar() : m(Traits::init()) {}
T m;
};
Last edited by D_Drmmr; October 24th, 2013 at 01:50 AM.
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
-
October 24th, 2013, 05:45 AM
#3
Re: template class and string default value
I may have misunderstood but are you looking for different default values per type? If so, this could be achieved by implementing the constructor for every type you intend to use with this class:
Code:
template <class T>
class A {
public:
A(); // Do not implement here
};
A<int>::A()
{
// Int specific set up here
}
A<float>::A()
{
// float specific set up here
}
Apologies if I'm barking up the wrong tree and good luck
Talaxy
-
October 24th, 2013, 07:07 AM
#4
Re: template class and string default value
typically speaking... you can provide default values for POD types.
strings are not PODs
so you can't make a default string-value.
there's ways around it (like using traits), but they'll be a bit more involved to implement.
A simple solution would be to extend your class to need an extra template parameter
the 2nd parameter can be any class that implements a
static std::string strvalue()
and where you need to initialisa your string, you call that function from the 2Nd class.
Code:
template <class T, class S>
class A {
public:
A() { version = S::strvalue(); }
std::string_base<T> version;
};
with
Code:
class Version1
{
public:
static std::string strvalue() { return "Version 1.0.0 Beta Super Pro Plusplus with Sugar on top"; }
};
and called as
Code:
int main()
{
A<int, Version1> myA;
}
-
October 24th, 2013, 07:17 AM
#5
Re: template class and string default value
Originally Posted by Talaxy
I may have misunderstood but are you looking for different default values per type? If so, this could be achieved by implementing the constructor for every type you intend to use with this class:
Code:
template <class T>
class A {
public:
A(); // Do not implement here
};
A<int>::A()
{
// Int specific set up here
}
A<float>::A()
{
// float specific set up here
}
I'm not sure if this is allowed in C++11, but in C++03 you cannot specialize a member function of a template class; you can only specialize the whole class. VC++ allows this, though.
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
-
October 24th, 2013, 07:24 AM
#6
Re: template class and string default value
Ah I see, I might be best sticking to the Visual C++ section then
Thanks!
-
October 27th, 2013, 08:12 PM
#7
Re: template class and string default value
Thank you very much for all your answers.
I will study the traits class, I am not very familiar with it but I feel that this is my best option.
Thanks again!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|