templated func : what's wrong

M

mosfet

HI,

when trying to compile an embedded version of STL called ustl on win32
platform I get the following error :

/// Returns the minimum of \p a and \p b
template <typename T1, typename T2>
inline const T1 min (const T1& a, const T2& b)
{
return (a < b ? a : b);
}

1>c:\program files\microsoft visual studio
8\vc\include\ustl\uutility.h(69) : error C2027: use of undefined type 'T1'
1>c:\program files\microsoft visual studio
8\vc\include\ustl\uutility.h(69) : error C2226: syntax error :
unexpected type 'T1'
1>c:\program files\microsoft visual studio
8\vc\include\ustl\uutility.h(69) : error C2988: unrecognizable template
declaration/definition
1>c:\program files\microsoft visual studio
8\vc\include\ustl\uutility.h(69) : error C2059: syntax error :
'<cv-qualifer>'
1>c:\program files\microsoft visual studio
8\vc\include\ustl\uutility.h(69) : error C2059: syntax error : ')'


I think it comes from the fact template functions like this are not
standard C++.
Could someone confirm ?

How can I change that ?
 
I

Ian Collins

mosfet said:
HI,

when trying to compile an embedded version of STL called ustl on win32
platform I get the following error :

/// Returns the minimum of \p a and \p b
template <typename T1, typename T2>
inline const T1 min (const T1& a, const T2& b)
{
return (a < b ? a : b);
}
There's nothing wrong with the above, maybe you have disabled templates?
I think it comes from the fact template functions like this are not
standard C++.
Could someone confirm ?
What's non-standard about it?
 
M

mosfet

Ian Collins a écrit :
There's nothing wrong with the above, maybe you have disabled templates?
What's non-standard about it?
I thought that now you cannot declare "global" templates without class
keyword.

When I look at MSDN doc here is whar I see :

The summary is optional.

The template declaration specifies a set of parameterized classes or
functions.


template < template-parameter-list > declaration


Remarks
The template-parameter-list is a comma-separated list of template
parameters, which may be types (in the form class identifier, typename
identifier, or template < template-parameter-list > class identifier) or
non-type parameters to be used in the template body. The syntax for a
template parameter is one of the following:

Copy Code
parameter-declaration
class identifier [ = typename ]
typename identifier [ = typename ]
template < template-parameter-list > class [identifier][= name]


You can instantiate a class template much like you would instantiate a
normal class, but you must include the template arguments within angle
brackets (<>). These template arguments can be any type if the template
argument list contains the class or typename keyword, or a value of the
appropriate type if the argument is a non-type argument. No special
syntax is required to call a function template, although the angle
brackets and template arguments can be required if the template
parameters cannot be deduced from the arguments to the function.

The template-parameter-list is a list of parameters used by the template
function that specifies which parts of the following code will vary. For
example:

Copy Code
template< class T, int i > class MyStack...


In this case, the template can receive a type (class T) and a constant
parameter (int i). The template will use type T and the constant integer
i upon instantiation. Within the body of the MyStack declaration, you
must refer to the T identifier.

A template declaration itself does not generate code; it specifies a
family of classes or functions, one or more of which will be generated
when referenced by other code.

Template declarations have global, namespace, or class scope. They
cannot be declared within a function.
 
I

Ian Collins

mosfet said:
Ian Collins a écrit :
I thought that now you cannot declare "global" templates without class
keyword.
You have written a function template, how were you using it to get that
error?

Something like:

int main() {
int a = 0;
short b = 3;

int c = min(a, b);
}
 
J

Jim Langston

mosfet said:
HI,

when trying to compile an embedded version of STL called ustl on win32
platform I get the following error :

/// Returns the minimum of \p a and \p b
template <typename T1, typename T2>
inline const T1 min (const T1& a, const T2& b)
{
return (a < b ? a : b);
}

1>c:\program files\microsoft visual studio
8\vc\include\ustl\uutility.h(69) : error C2027: use of undefined type 'T1'
1>c:\program files\microsoft visual studio
8\vc\include\ustl\uutility.h(69) : error C2226: syntax error : unexpected
type 'T1'
1>c:\program files\microsoft visual studio
8\vc\include\ustl\uutility.h(69) : error C2988: unrecognizable template
declaration/definition
1>c:\program files\microsoft visual studio
8\vc\include\ustl\uutility.h(69) : error C2059: syntax error :
'<cv-qualifer>'
1>c:\program files\microsoft visual studio
8\vc\include\ustl\uutility.h(69) : error C2059: syntax error : ')'


I think it comes from the fact template functions like this are not
standard C++.
Could someone confirm ?

How can I change that ?

I get a warning when I compile your code, I thought it wouldn't even
compile. When I remove the first "const" the error goes away. What is that
const suppoed to mean? Try this isntead:

template <typename T1, typename T2>
inline T1 min (const T1& a, const T2& b)
{
return (a < b ? a : b);
}

Note: changed from inline const T1 to inline T1

That const there doesn't make much sense to the compiler, or me.
 
J

joe

HI,

when trying to compile an embedded version of STL called ustl on win32
platform I get the following error :

/// Returns the minimum of \p a and \p b
template <typename T1, typename T2>
inline const T1 min (const T1& a, const T2& b)
{
return (a < b ? a : b);

}

1>c:\program files\microsoft visual studio
8\vc\include\ustl\uutility.h(69) : error C2027: use of undefined type 'T1'
1>c:\program files\microsoft visual studio
8\vc\include\ustl\uutility.h(69) : error C2226: syntax error :
unexpected type 'T1'
1>c:\program files\microsoft visual studio
8\vc\include\ustl\uutility.h(69) : error C2988: unrecognizable template
declaration/definition
1>c:\program files\microsoft visual studio
8\vc\include\ustl\uutility.h(69) : error C2059: syntax error :
'<cv-qualifer>'
1>c:\program files\microsoft visual studio
8\vc\include\ustl\uutility.h(69) : error C2059: syntax error : ')'

I think it comes from the fact template functions like this are not
standard C++.
Could someone confirm ?

How can I change that ?

Have you stumbled across a 'min' macro from else where? MS defines
one in the set of headers declared in windows.h. If you have, you can
fix that by invoking the function with the syntax

int a = (min)(5,6);

Surrounding the function name with parenthesis forces the preprocessor
to not match the function and to instead use the template.

joe
 
J

joe

HI,

when trying to compile an embedded version of STL called ustl on win32
platform I get the following error :

/// Returns the minimum of \p a and \p b
template <typename T1, typename T2>
inline const T1 min (const T1& a, const T2& b)
{
return (a < b ? a : b);

}

1>c:\program files\microsoft visual studio
8\vc\include\ustl\uutility.h(69) : error C2027: use of undefined type 'T1'
1>c:\program files\microsoft visual studio
8\vc\include\ustl\uutility.h(69) : error C2226: syntax error :
unexpected type 'T1'
1>c:\program files\microsoft visual studio
8\vc\include\ustl\uutility.h(69) : error C2988: unrecognizable template
declaration/definition
1>c:\program files\microsoft visual studio
8\vc\include\ustl\uutility.h(69) : error C2059: syntax error :
'<cv-qualifer>'
1>c:\program files\microsoft visual studio
8\vc\include\ustl\uutility.h(69) : error C2059: syntax error : ')'

I think it comes from the fact template functions like this are not
standard C++.
Could someone confirm ?

How can I change that ?

Btw, it compiles for me as is on VC 8.0

joe
 
B

Bo Persson

joe wrote:
::: HI,
:::
::: when trying to compile an embedded version of STL called ustl on
::: win32 platform I get the following error :
:::
::: /// Returns the minimum of \p a and \p b
::: template <typename T1, typename T2>
::: inline const T1 min (const T1& a, const T2& b)
::: {
::: return (a < b ? a : b);
:::
::: }
:::
::: 1>c:\program files\microsoft visual studio
::: 8\vc\include\ustl\uutility.h(69) : error C2027: use of undefined
::: type 'T1' 1>c:\program files\microsoft visual studio
::: 8\vc\include\ustl\uutility.h(69) : error C2226: syntax error :
::: unexpected type 'T1'
::: 1>c:\program files\microsoft visual studio
::: 8\vc\include\ustl\uutility.h(69) : error C2988: unrecognizable
::: template declaration/definition
::: 1>c:\program files\microsoft visual studio
::: 8\vc\include\ustl\uutility.h(69) : error C2059: syntax error :
::: '<cv-qualifer>'
::: 1>c:\program files\microsoft visual studio
::: 8\vc\include\ustl\uutility.h(69) : error C2059: syntax error : ')'
:::
::: I think it comes from the fact template functions like this are
::: not standard C++.
::: Could someone confirm ?
:::
::: How can I change that ?
::
:: Btw, it compiles for me as is on VC 8.0
::
:: joe

It depends on the settings, and what headers you include.

On VC way to get rid of these macros is the define the macro NOMINMAX
in you project settings.


Bo Persson
 

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
474,470
Messages
2,571,809
Members
48,797
Latest member
PeterSimpson
Top