Difficulty with "Local Types as Template Arguments"

T

towi

Hello,

I noticed that C++0x will allow the following:

void func() {
struct Op { // locally defined type
void operator()(int arg) { /*...*/ }
};
std::for_each(..., Op()); // using Op as template argument
}

where before it was prohibited to use "Op" there because it is a
template argument there.

I wonder... why was this not included in C++98? What is/was the main
challange in allowing this for compiler implementors?

I have the feeling it has something to do with scopes an visibility of
variables. Something like that in C (and C++98) averything a Functor/
Function can operate on is visible at a global scope. Allowing this
would introduce a complex scenario of making things visible, to where
a Functor is called, what was initially only visible locally where the
Functor is defined. I am aware that this is a challange in Pascal and
a very distinguishing feature of Pascal versus C (and C++98).
Is this correct, or are there other challanges to overcome -- or
reasons why this was not included in C++98.

Thanks in advance,

tschau, towi.
 
A

Anthony Williams

towi said:
Hello,

I noticed that C++0x will allow the following:

void func() {
struct Op { // locally defined type
void operator()(int arg) { /*...*/ }
};
std::for_each(..., Op()); // using Op as template argument
}

where before it was prohibited to use "Op" there because it is a
template argument there.

I wonder... why was this not included in C++98? What is/was the main
challange in allowing this for compiler implementors?

I believe it was excluded from C++98 "to be on the safe side" because
implementors had concerns.

e.g.

void f()
{
struct X{ int i; double d;};
std::vector<X> vec;
}

void g()
{
struct X{ std::string s;};
std::vector<X> vec;
}

These two types X are different, and must yield distinct instantiations
of std::vector<X>.

Also

inline void h()
{
struct X{};
std::vector<X> vec;
}

This std::vector<X> must be the *same* instantiation for all translation
units that include this definition of h().

It was only included in C++0x because I and others campaigned to have it
added. IIRC, it *will* cause a breaking change in the ABI for some
compilers, but it is not the only C++0x feature to do so.

Anthony
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top