473,473 Members | 2,178 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Local class instances invalid template argument

Hello,

I thought one major advantage of using functors as e.g. sorting
predicates over functions would be that I can do something like this:

void foo()
{
class Predicate
{
public:
bool operator() (...) {...}
};

std::list<whatever> lst;
// ...
std::transform( lst.begin(), lst.end(), lst.begin(), Predicate() );
}

However, I am getting a compile time error "type foo()::Predicate
composed from a local class is not a valid template-argument".

Why?

--
Regards,
Matthias
Jul 23 '05 #1
10 7295
Matthias wrote:
Hello,

I thought one major advantage of using functors as e.g. sorting
predicates over functions would be that I can do something like this:

void foo()
{
class Predicate
{
public:
bool operator() (...) {...}
};

std::list<whatever> lst;
// ...
std::transform( lst.begin(), lst.end(), lst.begin(), Predicate() );
}

However, I am getting a compile time error "type foo()::Predicate
composed from a local class is not a valid template-argument".

Why?


Whoops, this should read:

lst.sort( ..., Predicate() );

--
Regards,
Matthias
Jul 23 '05 #2

Matthias wrote:
class Predicate
{
public:
bool operator() (...) {...}
};


Make sure it takes the correct arguments.

-shez-

Jul 23 '05 #3
Shezan Baig wrote:
Matthias wrote:
class Predicate
{
public:
bool operator() (...) {...}
};

Make sure it takes the correct arguments.

-shez-


When taking the exact same class outside the function scope, everything
works just fine. I doubt it had something to do with the arguments.

--
Regards,
Matthias
Jul 23 '05 #4
Hi,

Matthias wrote:
Hello,

I thought one major advantage of using functors as e.g. sorting
predicates over functions would be that I can do something like this:

void foo()
{
class Predicate
{
public:
bool operator() (...) {...}
};

std::list<whatever> lst;
// ...
std::transform( lst.begin(), lst.end(), lst.begin(), Predicate() ); }

However, I am getting a compile time error "type foo()::Predicate
composed from a local class is not a valid template-argument".

Why?

Because the standard say so:

14.3.1 paragraph 2:
"A local type, a type with no linkage, an unnamed type or a type
compounded from any of these types shall not be used as a
templateargument for a template typeparameter.
[Example:
template <class T> class X { /* ... */ };
void f()
{
struct S { /* ... */ };
X<S> x3; // error: local type used as templateargument
X<S*> x4; // error: pointer to local type used as templateargument
}
-end example] [Note: a template type argument may be an incomplete
type (3.9). ]"

But, see the following link:
http://groups-beta.google.com/group/...m&hl=en&rnum=2
..
..
Best regards,
Bogdan Sintoma

Jul 23 '05 #5
Bogdan Sintoma wrote:
Hi,

Matthias wrote:
Hello,

I thought one major advantage of using functors as e.g. sorting
predicates over functions would be that I can do something like this:

void foo()
{
class Predicate
{
public:
bool operator() (...) {...}
};

std::list<whatever> lst;
// ...
std::transform( lst.begin(), lst.end(), lst.begin(), Predicate()


);
}

However, I am getting a compile time error "type foo()::Predicate
composed from a local class is not a valid template-argument".

Why?


Because the standard say so:

14.3.1 paragraph 2:
"A local type, a type with no linkage, an unnamed type or a type
compounded from any of these types shall not be used as a
templateargument for a template typeparameter.
[Example:
template <class T> class X { /* ... */ };
void f()
{
struct S { /* ... */ };
X<S> x3; // error: local type used as templateargument
X<S*> x4; // error: pointer to local type used as templateargument
}
-end example] [Note: a template type argument may be an incomplete
type (3.9). ]"

But, see the following link:
http://groups-beta.google.com/group/...m&hl=en&rnum=2
.
.
Best regards,
Bogdan Sintoma


Ah, that's clever, in fact simply making the local type also a non-local
type by subtyping it from a non-local type.
I wonder if you need the additional overhead this delegation produces at
all. Couldn't you just inherit from an empty non-local class? That would
make your type non-local as well, and you wouldn't have to do that
delegation stuff... Just an idea.

--
Regards,
Matthias
Jul 23 '05 #6
Matthias wrote:
Bogdan Sintoma wrote:
Hi,

Matthias wrote:
Hello,

I thought one major advantage of using functors as e.g. sorting
predicates over functions would be that I can do something like this:

void foo()
{
class Predicate
{
public:
bool operator() (...) {...}
};

std::list<whatever> lst;
// ...
std::transform( lst.begin(), lst.end(), lst.begin(), Predicate()

);
}

However, I am getting a compile time error "type foo()::Predicate
composed from a local class is not a valid template-argument".

Why?


Because the standard say so:

14.3.1 paragraph 2:
"A local type, a type with no linkage, an unnamed type or a type
compounded from any of these types shall not be used as a
templateargument for a template typeparameter.
[Example:
template <class T> class X { /* ... */ };
void f()
{
struct S { /* ... */ };
X<S> x3; // error: local type used as templateargument
X<S*> x4; // error: pointer to local type used as templateargument
}
-end example] [Note: a template type argument may be an incomplete
type (3.9). ]"

But, see the following link:
http://groups-beta.google.com/group/...m&hl=en&rnum=2

.
.
Best regards,
Bogdan Sintoma


Ah, that's clever, in fact simply making the local type also a non-local
type by subtyping it from a non-local type.
I wonder if you need the additional overhead this delegation produces at
all. Couldn't you just inherit from an empty non-local class? That would
make your type non-local as well, and you wouldn't have to do that
delegation stuff... Just an idea.


Ah yes of course, nevermind. Got a misunderstanding there, but hey, it
works :)

--
Regards,
Matthias
Jul 23 '05 #7
Bogdan Sintoma wrote:
But, see the following link:
http://groups-beta.google.com/group/...m&hl=en&rnum=2
.
.
Best regards,
Bogdan Sintoma


Hm, I am getting an error:
localclass.cpp: In constructor `FunctorWrapper<T>::FunctorWrapper(const
FunctorBase<T>&) [with T = int]':
localclass.cpp:39: instantiated from here
localclass.cpp:17: error: invalid initialization of reference of type '
FunctorBase<int>&' from expression of type 'const FunctorBase<int>'

Here is the code (I have taken out the abstraction of the return type):

template<typename T>
class FunctorBase
{
public:
virtual bool operator() ( const T& ) const = 0;
};

template<typename T>
class FunctorWrapper
{
FunctorBase<T>& _functor;
public:
FunctorWrapper( const FunctorBase<T>& rf )
: _functor(rf) {}

bool operator() ( const T& t ) const {
return _functor(t);
}
};
int main()
{
std::list<int> l;
class Functor: public FunctorBase<int>
{
public:
bool operator() (const int& n) const {
return true;
}
};

Functor f;
FunctorWrapper<int> w(f);

std::transform( l.begin(), l.end(), l.begin(), w );
}
--
Regards,
Matthias
Jul 23 '05 #8
Matthias wrote:
Here is the code (I have taken out the abstraction of the return type):
template<typename T>
class FunctorBase You should derive the FunctorBase from unary_function<T, bool>.
{
public:
virtual bool operator() ( const T& ) const = 0;
};

template<typename T>
class FunctorWrapper
{
FunctorBase<T>& _functor;
public:
FunctorWrapper( const FunctorBase<T>& rf )

Here is the problem: ^^^
Either you store your _functor as a const reference either you modify
the constructor to receive a non-const reference. And avoid the leading
underscore ;).

Jul 23 '05 #9
Bogdan Sintoma wrote:
Matthias wrote:
Here is the code (I have taken out the abstraction of the return
type):
template<typename T>
class FunctorBase


You should derive the FunctorBase from unary_function<T, bool>.


Why?
{
public:
virtual bool operator() ( const T& ) const = 0;
};

template<typename T>
class FunctorWrapper
{
FunctorBase<T>& _functor;
public:
FunctorWrapper( const FunctorBase<T>& rf )
Here is the problem: ^^^
Either you store your _functor as a const reference either you modify
the constructor to receive a non-const reference.


Oh, of course :) Thanks.

And avoid the leading underscore ;).


Why? It's my way of notating a member variable. This way you directly
see which variable is a member and which is not. I used to use m_ but _
is shorter :)

--
Regards,
Matthias
Jul 23 '05 #10
Matthias wrote:
You should derive the FunctorBase from unary_function<T, bool>.

Why?

Again, because the c++ standard say so ;)
Chapter 20.3 paragraph 5.
"To enable adaptors and other components to manipulate function objects
that take one or two arguments it is required that the function objects
correspondingly provide typedefs argument_type and result_type for
function objects that take one argument and first_argument_type,
second_argument_type, and result_type for function objects that take
two arguments."

You can just provide those typedefs, but that's exactly what
unary_function is for.

And avoid the leading underscore ;).

Why? It's my way of notating a member variable. This way you directly
see which variable is a member and which is not.
I used to use m_ but _ is shorter :)


According to the standard:
"17.4.3.1.2 Global names
1 Certain sets of names and function signatures are always reserved to
the implementation:
- Each name that contains a double underscore (_ _) or begins with an
underscore followed by an uppercase letter (2.11) is reserved to the
implementation for any use.
- Each name that begins with an underscore is reserved to the
implementation for use as a name in the
global namespace.165)"

So, not every name that start with an underscore is restricted, but it
is more easy to avoid leading underscore than to keep in mind all of
above :).

....
Bogdan

Jul 23 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Kerry Neilson | last post by:
Hi, Really hung up on this one. I'm trying to get all the fields of a dictionary to be unique for each class: class A { my_dict = dict_entry = { 'key1':0, 'key2':0 } __init__(self): for...
2
by: Gabriel Schreiber | last post by:
Hi this code: #include <vector> void f() { struct arg{}; std::vector<arg> v; };
24
by: Brad Marts | last post by:
I would like to have a function that takes as an argument a base class but performs differently depending on which type of derived class is passed. Can I tell which derived class is passed? For...
6
by: Johan Bergman | last post by:
Hi, Maybe someone can help me with this one. The following describes a somewhat simplified version of my problem, but I think it will be sufficient. I want to use class factories (virtual...
4
by: Gert Van den Eynde | last post by:
Hi all, A beginners question.... I've got a template class template <class T> classA {...} In an other class, I want to pass a pointer to an instance of classA as a function argument....
17
by: Steven T. Hatton | last post by:
I didn't think this was possible in C++. I don't believe Stroustrup mentions this anywhere in TC++PL(SE). I've never seen it used in code that I can recall. Is this feature widely supported? Is...
16
by: PengYu.UT | last post by:
Hi, I want to partial specialize the member function doit. But it doesn't work. Could you please help me to figure out what is wrong? Thanks, Peng template <typename T> class A {
28
by: cpluslearn | last post by:
Hi, I have a local class inside a function template. I am able to wrap any type in my local class. Is it legal C++? What type of class is Local? Is it a class template or regular class? Thanks...
2
by: amkg | last post by:
Hello world, I'm trying to figure out some way to constrain a template class argument for a member function definition, such that the template class argument must, totally, absolutely be a...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.