Question

Unfortunately i have lost the link and the source for this article, but I do remember that it was about metaprogramming and templates in C++; when talking about the limitations around the template approach, this article was speaking about the fact that what is passed to a template it's not a generic type, but only types with a related address ( or addressable, I don't remember the exact words ) can be passed as arguments to a template.

Can someone clarify this relation between addresses, types and templates ?

A type can be identified with an address ?


EDIT

for example at this link, in the last part of the linked FAQ, the template system is described like something that takes an address rather than a generic type.

Was it helpful?

Solution

I don't know about the original article you read, but the FAQ you linked to in the edit does not talk about a connection between types and addresses.

It talks about one specific kind of template parameters, the non-type parameters. There are three kinds of template parameters: type, non-type and template. So this is specifically about the second kind.

An example of a non-type parameter is Id in the definition below:

template <const char *Id>
struct C
{};

Id does not represent a type, but a non-type, i.e. an actual value. In this particular example, that value happens to be of a a pointer type, and it represents the address of a string.

The idea is that you can use this to instantiate the template using a string as distinguisher:

C<"foo"> c1;  // Instantiating the "foo" version of type C
C<"bar"> c2;  // Instantiating the "bar" version of type C

Unfortunately, it does not work like this – and that is what the FAQ article is about. It explains that for a non-type template parameter, you must use a constant expression, and in this particular case, you must use an identifier of an object with external linkage, not just a literal. So the only way you can do it is this:

template <const char *Id>
struct C
{

};

char id1[] = "foo";
char id2[] = "bar";

int main()
{
  C<id1> c1;
  C<id2> c2;
  /*...*/
  return 0;
}

That is what the FAQ article explains.

However, again, this is a very special case, because it applies to non-type parameters of an address type only. Note that the connection to addresses is given because the non-type parameter is explicitly specified as a parameter of pointer type const char *.

Most usual templates arguably use either a type-parameter or a non-type parameter of an integral, enum, or user-defined type. None of those has anything to do with memory addresses. There is no implicit connection between using templates and using memory addresses.

OTHER TIPS

Here is an example of a type identified with an address:

int foo;

template <int *IP>
class Bar {
};

Bar<&foo> baz;

But somehow, I don't think that's what the article meant.

Types themselves do not have addresses in C++. Individual definitions within a type may or may not have addresses of their own. And some things only have addresses within an instance of the type.

The trick I used up there can only be used on objects that exist for the entire lifetime of the program. And then the address of the object becomes part of the type's name.

And templates are not even types in C++. They are only a type when they've been instantiated with a particular set of parameters. And they don't have addresses either.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top