-
January 23rd, 2013, 12:00 AM
#1
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/
Last edited by aamir121a; January 23rd, 2013 at 03:36 AM.
-
January 23rd, 2013, 07:17 AM
#2
Re: C++ templates without arguments
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.
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;
}
"It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
Richard P. Feynman
-
January 23rd, 2013, 07:59 AM
#3
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.
-
January 23rd, 2013, 09:21 AM
#4
Re: C++ templates without arguments
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.
-
January 23rd, 2013, 10:09 AM
#5
Re: C++ templates without arguments
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.
"It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
Richard P. Feynman
-
January 26th, 2013, 08:55 PM
#6
Re: C++ templates without arguments
Thank you john and OReubens
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
|