Greetings.
I am looking through some of the examples in Stroustrup's book and he has
certain examples of doing compile time checks for a variety of things.
One of them being the following...
template<class T, class B> struct Derived_from {
static void constraints(T* p) { B* pb = p; }
Derived_from() { void(*p)(T*) = constraints; }
};
struct B { };
struct D : B { };
struct DD : D { };
struct X { };
int main()
{
Derived_from<D,B>();
Derived_from<DD,B>();
Derived_from<X,B>();
}
Obviously the first, two get complied and the third one doesn't, which is
obvious looking at the code.
However, my question is about the template and how this whole thing works.
Basically in the above template, p is defined to be a function pointer and
is assigned to the function 'constraints'.
Not at any point is the function being called, to actually evaluate if the
assignment is being actually done or not. I know this is related to the way
templates and function templates are instantiated(only when they are
referenced). But, I am just trying to understand how exactly this is done.
So, basically when the compiler sees the reference to the "constraints'
function, it instantiates that template function for the said parameters and
checks whether the code is being compiled ?
A little more explanation would be more helpful.
I know this is also mentioned in the Vandervoode book about Templates and
how the instantiation is done, but I don't have that book with me right now.
Thanks. 1 1647
The third template instantiation fails. This is because, when the
compiler encounters the statement, Derived_from<X,B>();
It will have to generate an appropriate template instance for
'Derived_from' that takes 'struct X' and 'struct B' as its template
parameters. This would look something like this.
struct Derived_from {
static void constraints(X* p) { B* pb = p; }
Derived_from() { void(*p)(T*) = constraints; }
};
In this piece of code, X is not derived from B. So, the assignment
B* pb = p;
will fail. There's no way to convert from X* to B*. Once the particular
template instance is generated, it'll be compiled to verify
correctness. Since the pointer assignment fails, the compiler will
throw up an error.
Hope this was a clear explanation
Regards,
Srini
Amit wrote: Greetings.
I am looking through some of the examples in Stroustrup's book and he
has certain examples of doing compile time checks for a variety of
things. One of them being the following...
template<class T, class B> struct Derived_from { static void constraints(T* p) { B* pb = p; } Derived_from() { void(*p)(T*) = constraints; } };
struct B { }; struct D : B { }; struct DD : D { }; struct X { };
int main() { Derived_from<D,B>(); Derived_from<DD,B>(); Derived_from<X,B>(); }
Obviously the first, two get complied and the third one doesn't,
which is obvious looking at the code. However, my question is about the template and how this whole thing
works. Basically in the above template, p is defined to be a function
pointer and is assigned to the function 'constraints'. Not at any point is the function being called, to actually evaluate
if the assignment is being actually done or not. I know this is related to
the way templates and function templates are instantiated(only when they are referenced). But, I am just trying to understand how exactly this is
done. So, basically when the compiler sees the reference to the
"constraints' function, it instantiates that template function for the said
parameters and checks whether the code is being compiled ? A little more explanation would be more helpful.
I know this is also mentioned in the Vandervoode book about Templates
and how the instantiation is done, but I don't have that book with me
right now. Thanks. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Ben Ingram |
last post by:
Hi all,
I am writing a template matrix class in which the template parameters are
the number of rows and number of columns. There are a number of...
|
by: Leon |
last post by:
Hi all.
I'm using a thrid-party software library that uses two tag classes (Tag_true
and Tag_false) to flag if certain operations are available...
|
by: Thomi Richards |
last post by:
Hi,
I'm trying to create a simple stack class using C++ and templates.
Everything works well and good if I amke the class totally inline....
|
by: Bart Goeman |
last post by:
Hi,
I have a question about how to put redundant information in data
structures, initialized at compile time. This is often necessary
for...
|
by: Glen |
last post by:
I'm working on a custom assembly and I'm trying to figure out the best
approach to handling known constraints within the assembly, once
compiled,...
|
by: Dave Rahardja |
last post by:
I have the following program that uses an array of chars to simulate a bit
set:
---------
// An out-of-bounds exception
class BoundsException...
|
by: andrewcw |
last post by:
I am moving some code forward from .NET 1.1. I was able to load the XSL file
and perform the transform. The MSDN documentation looks like it...
|
by: desktop |
last post by:
I have read that using templates makes types know at compile time and
using inheritance the types are first decided at runtime. The use of
pointers...
|
by: parag_paul |
last post by:
I have been seeing a consistent slowness in g++ compilation for a
small file like the following , The uptime is near ( load Is near to
0 ) . I have...
|
by: Kemmylinns12 |
last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
|
by: Naresh1 |
last post by:
What is WebLogic Admin Training?
WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
|
by: jalbright99669 |
last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
|
by: antdb |
last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine
In the overall architecture, a new "hyper-convergence" concept was...
|
by: Matthew3360 |
last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function.
Here is my code.
...
|
by: Matthew3360 |
last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
|
by: Arjunsri |
last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
|
by: Matthew3360 |
last post by:
Hi,
I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
|
by: BLUEPANDA |
last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...
| |