By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,470 Members | 924 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,470 IT Pros & Developers. It's quick & easy.

dependent template parameter

P: n/a
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);
}
Sep 15 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Nate Barney wrote:
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

Sep 15 '06 #2

P: n/a
Thomas Tutone wrote:
Nate Barney wrote:
>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.
Nifty, thanks.
>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.
Oops, I had it in there, but I guess I accidentally erased it when I
wrapped the code for usenet.

Thanks again,
Nate
Sep 15 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.