C++ templates without arguments
Some reading through C++ code I come across template deceleration without any type , I have tested the code it compiles too
Code:
template<>
class abc
{
}
similarly template usage
could some one please why this is so.
example of such usage can be found here
http://www.boost.org/doc/libs/1_39_0/libs/random/
Re: C++ templates without arguments
Quote:
Originally Posted by
aamir121a
Some reading through C++ code I come across template deceleration without any type , I have tested the code it compiles too
Code:
template<>
class abc
{
}
It doesn't for me.
Quote:
explicit specialization; 'abc' is not a specialization of a class template
This does compile. Are you sure you didn't mean this?
Code:
template<typename T = int>
class abc
{
};
int main()
{
abc<> x;
}
Re: C++ templates without arguments
I am using VS 2008 , however you are right when I cleaned the project and rebuild it does not work. ( however your code works fine ) however I still don't understand why template are being used this way , to my understanding the whole idea of template is create a generic framework which can be applied to any data type.
Re: C++ templates without arguments
Quote:
Originally Posted by
aamir121a
... which can be applied to any data type.
This isn't entirely true.
Templates are not required to work for "any type", they are work for any type that is compatible with what the templated is designed for.
The compiler will "try" to substitute whatever type you provide as template parameter, but this can very much result in a compiler error if this type doesn't conform to what the template expects.
Re: C++ templates without arguments
Quote:
to my understanding the whole idea of template is create a generic framework which can be applied to any data type.
The template parameter doesn't even have to be a type.
Here's a template with a non-type parameter, with defaults.
Code:
template <const size_t SIZE = 1024>
class FixedLengthContainer
{
};
FixedLengthContainer<> container1; // Create a default 1K sized object.
FixedLengthContainer<1048576> container2; // Create a 1M sized object.
Re: C++ templates without arguments
Thank you john and OReubens