dependent template parameter

N

Nate Barney

Is it acceptable, according to the standard, to make the type of a
template parameter a dependent type of a previous template parameter in
the same template declaration?

I tried this on a lark, just to see what would happen. To my surprise,
it compiled and ran correctly under g++ 4.1.1.

#include <iostream>
#include <iterator>

template <typename It
typename std::iterator_traits<It>::difference_type N>
void print(It it)
{
for (int i = 0; i < N; ++i)
std::cout << *it++ << std::endl;
}

int main()
{
int a[] = { 0, 1, 2, 3, 4 };

print<const int*,5>(a);
}
 
T

Thomas Tutone

Nate said:
Is it acceptable, according to the standard, to make the type of a
template parameter a dependent type of a previous template parameter in
the same template declaration?

Yes. I believe the Standard Library uses this feature for, among other
things, the standard containers.
I tried this on a lark, just to see what would happen. To my surprise,
it compiled and ran correctly under g++ 4.1.1.

#include <iostream>
#include <iterator>

template <typename It

You're missing a comma at the end of the above line.
typename std::iterator_traits<It>::difference_type N>
void print(It it)
{
for (int i = 0; i < N; ++i)
std::cout << *it++ << std::endl;
}

int main()
{
int a[] = { 0, 1, 2, 3, 4 };

print<const int*,5>(a);
}

It's valid code if you fix the typo mentioned above.

Best regards,

Tom
 
N

Nate Barney

Thomas said:
Yes. I believe the Standard Library uses this feature for, among other
things, the standard containers.

Nifty, thanks.
You're missing a comma at the end of the above line.

Oops, I had it in there, but I guess I accidentally erased it when I
wrapped the code for usenet.

Thanks again,
Nate
 

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top