Why does this template code compile?

P

Peter

In "C++ Templates The Complete Guide" book by Vandevoorde
and Josuttis I found the following template definition
near the end of chapter 8.2 ("Template Arguments"):

template<template<typename T, T*> class Buf>
class Lexer
{
static char storage[5];
Buf<char, &Lexer<Buf>::storage> buf;
};

Buf is a template template argument whose template
arguments are: type parameter T and unnamed non-type
parameter - a pointer to T. In the definition of Lexer,
Buf is instantiated so that T = char and second argument
(of type T*) is &Lexer<Buf>::storage, but why is this
second substitution correct?
How does &Lexer<Buf>::storage resolve to char*?

Here's my reasoning.

Lex<Buf>::storage is of type char[5],
&Lex<Buf>::storage is of type char(*)[5].

If we had:

Buf<char, Lexer<Buf>::storage> buf;

instead of:

Buf<char, &Lexer<Buf>::storage> buf;

in the definition of Lexer template then
Lexer<Buf>::storage would decay to char*, but why
can the template be instantiated with
&Lexer<Buf>::storage as well? I tested both definitions
here:

http://www.compileonline.com/compile_cpp11_online.php

and they both compile.

However, if I try to instantiate Lexer with
&Lexer<Buf>::storage as second argument to Buf, I get
a compilation error as expected. Here's a complete example:


template<template<typename T, T*> class Buf>
class Lexer
{
static char storage[5];
Buf<char, &Lexer<Buf>::storage> buf;
};

template<typename T, T*>
class Foo
{
};

int main()
{
Lexer<Foo> lex;
return 0;
}

Now:

1) if I try to compile the above I get the following error:

main.cpp: In instantiation of 'class Lexer<Foo>':
main.cpp:15:15: required from here
main.cpp:5:37: error: could not convert template argument
'& Lexer<Foo>::storage' to 'char*'

2) if I change "&Lexer<Buf>::storage" to
"Lexer<Buf>::storage" the code compiles

3) if I comment out the instantiation of Lexer from main()
the code compiles with either &Lexer<Buf>::storage or
Lexer<Buf>::storage as second argument to Buf.

Can you explain what happens here and why Buf template can
be instantiated with either of the two arguments?
 
V

Victor Bazarov

In "C++ Templates The Complete Guide" book by Vandevoorde
and Josuttis I found the following template definition
near the end of chapter 8.2 ("Template Arguments"):

template<template<typename T, T*> class Buf>
class Lexer
{
static char storage[5];
Buf<char, &Lexer<Buf>::storage> buf;
};

Buf is a template template argument whose template
arguments are: type parameter T and unnamed non-type
parameter - a pointer to T. In the definition of Lexer,
Buf is instantiated so that T = char and second argument
(of type T*) is &Lexer<Buf>::storage, but why is this
second substitution correct?
How does &Lexer<Buf>::storage resolve to char*?

I think it doesn't "resolve". Not until you actually instantiate the
template.
Here's my reasoning.

Lex<Buf>::storage is of type char[5],
&Lex<Buf>::storage is of type char(*)[5].

No. It's not. &(Lex<Buf>::storage) would be a pointer to an array.
But since the parentheses are missing (see 5.3.1), the expression is a
pointer-to-member.
If we had:

Buf<char, Lexer<Buf>::storage> buf;

instead of:

Buf<char, &Lexer<Buf>::storage> buf;

in the definition of Lexer template then
Lexer<Buf>::storage would decay to char*, but why
can the template be instantiated with
&Lexer<Buf>::storage as well? I tested both definitions
here:

http://www.compileonline.com/compile_cpp11_online.php

and they both compile.

Unless you try to instantiate the 'Lexer' template, the code only
undergoes syntax check and not the actual compilation (no conversions
are attempted).
However, if I try to instantiate Lexer with
&Lexer<Buf>::storage as second argument to Buf, I get
a compilation error as expected. Here's a complete example:


template<template<typename T, T*> class Buf>
class Lexer
{
static char storage[5];
Buf<char, &Lexer<Buf>::storage> buf;
};

template<typename T, T*>
class Foo
{
};

int main()
{
Lexer<Foo> lex;
return 0;
}

Now:

1) if I try to compile the above I get the following error:

main.cpp: In instantiation of 'class Lexer<Foo>':
main.cpp:15:15: required from here
main.cpp:5:37: error: could not convert template argument
'& Lexer<Foo>::storage' to 'char*'

2) if I change "&Lexer<Buf>::storage" to
"Lexer<Buf>::storage" the code compiles

3) if I comment out the instantiation of Lexer from main()
the code compiles with either &Lexer<Buf>::storage or
Lexer<Buf>::storage as second argument to Buf.

Which most likely means that the conversion is never attempted.
Can you explain what happens here and why Buf template can
be instantiated with either of the two arguments?

I can't explain how 'Buf' can be instantiated if it acutally can't be
instantiated (you get the error when you try to instantiate it).

V
 
A

Alf P. Steinbach

In "C++ Templates The Complete Guide" book by Vandevoorde
and Josuttis I found the following template definition
near the end of chapter 8.2 ("Template Arguments"):

template<template<typename T, T*> class Buf>
class Lexer
{
static char storage[5];
Buf<char, &Lexer<Buf>::storage> buf;
};

Buf is a template template argument whose template
arguments are: type parameter T and unnamed non-type
parameter - a pointer to T. In the definition of Lexer,
Buf is instantiated so that T = char and second argument
(of type T*) is &Lexer<Buf>::storage, but why is this
second substitution correct?

It isn't.

How does &Lexer<Buf>::storage resolve to char*?

It doesn't. The code is ungood.

Here's my reasoning.

Lex<Buf>::storage is of type char[5],
&Lex<Buf>::storage is of type char(*)[5].
Yep.


If we had:

Buf<char, Lexer<Buf>::storage> buf;

instead of:

Buf<char, &Lexer<Buf>::storage> buf;

in the definition of Lexer template then
Lexer<Buf>::storage would decay to char*, but why
can the template be instantiated with
&Lexer<Buf>::storage as well?

It can't.

I tested both definitions here:

http://www.compileonline.com/compile_cpp11_online.php

and they both compile.

Strange, it uses g++ 4.7.2 and that template definition (when
instantiated) does not compile with g++ 4.7.2 on my system.

Wait, lemme test that with the online compiler...

Nope, it failed:

main.cpp: In instantiation of 'class Lexer<Foo>':
main.cpp:15:15: required from here
main.cpp:5:36: error: could not convert template argument '&
Lexer<Foo>::storage' to 'char*'
Buf<char, &Lexer<Buf>::storage> buf;


However, if I try to instantiate Lexer with
&Lexer<Buf>::storage as second argument to Buf, I get
a compilation error as expected.

This doesn't make sense. It's the &Lexer<Buf>::storage that you claimed
(apparently incorrectly) compiled fine.

Here's a complete example:


template<template<typename T, T*> class Buf>
class Lexer
{
static char storage[5];
Buf<char, &Lexer<Buf>::storage> buf;
};

template<typename T, T*>
class Foo
{
};

int main()
{
Lexer<Foo> lex;
return 0;
}

Now:

1) if I try to compile the above I get the following error:

main.cpp: In instantiation of 'class Lexer<Foo>':
main.cpp:15:15: required from here
main.cpp:5:37: error: could not convert template argument
'& Lexer<Foo>::storage' to 'char*'

2) if I change "&Lexer<Buf>::storage" to
"Lexer<Buf>::storage" the code compiles

Yes, of course.

3) if I comment out the instantiation of Lexer from main()
the code compiles with either &Lexer<Buf>::storage or
Lexer<Buf>::storage as second argument to Buf.

Template definitions get only superficial analysis until they're
instantiated.

Can you explain what happens here

Yes, the claim at the start, of the code compiling with the online
compiler, appears to be incorrect.

and why Buf template can
be instantiated with either of the two arguments?

It can't, it's a bug (of the typo kind) in the code.

I suggest reporting it to the book's errata list (check first if it's
there already):

http://www.josuttis.com/tmplbook/errata.html


Cheers & hth.,

- Alf
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,059
Latest member
cryptoseoagencies

Latest Threads

Top