473,509 Members | 2,912 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

template question

Hi,

given the next code:
#define ENUM_CAST(x) int(x)

template<class T,int N>
class Array
{
public:
enum { rank = N };

protected:

// members ...

template<bool>
struct Select{ };

public:

template<class T_expr>
Array& evaluate(T_expr expr, Select<true>& )
{
// ... code
return *this;
}

template<class T_expr>
Array& evaluate(T_expr expr, Select<false>& )
{
// ... code
return *this;
} template<class T_expr>
Array& evaluateExpr(T_expr expr)
{
return
evaluate(expr,Select<ENUM_CAST(rank)==ENUM_CAST(T_ expr::rank)>());
}

public:
// ctors, dtor, function members ...
};

template<class T,int N>
class SomeExpr
{
public:
enum { rank = N };
};

void testFunc()
{
Array<double,2> A;
SomeExpr<double,2> x;
SomeExpr<double,1> y;

A.evaluateExpr(x);
A.evaluateExpr(y);

}

Intel c++ for linux and g++ 3.3.1 do not compile the code (the error
messages are below). In the meantime Intel c++ for Windows compiles it.
If I change the evaluate member functions like below

template<class T_expr>
Array& evaluate(T_expr expr, Select<true> )
{
// ... code
return *this;
}

template<class T_expr>
Array& evaluate(T_expr expr, Select<false> )
{
// ... code
return *this;
}
the compilation succedes.

Which compiler is right ?

Here is the output of the compiler (g++ 3.3.1):

stest.cpp: In member function `Array<T, N>& Array<T,
N>::evaluateExpr(T_expr) [with T_expr = SomeExpr<double, 2>, T = double,
int N = 2]':
stest.cpp:57: instantiated from here
stest.cpp:36: error: no matching function for call to `Array<double,
2>::evaluate(SomeExpr<double, 2>&, Array<double, 2>::Select<true>)'
stest.cpp:21: error: candidates are: Array<T, N>& Array<T,
N>::evaluate(T_expr, Array<T, N>::Select<true>&) [with T_expr =
SomeExpr<double, 2>, T = double, int N = 2]
stest.cpp:28: error: Array<T, N>& Array<T,
N>::evaluate(T_expr, Array<T, N>::Select<false>&) [with T_expr =
SomeExpr<double, 2>, T = double, int N = 2]
stest.cpp: In member function `Array<T, N>& Array<T,
N>::evaluateExpr(T_expr) [with T_expr = SomeExpr<double, 1>, T = double,
int N = 2]':
stest.cpp:58: instantiated from here
stest.cpp:36: error: no matching function for call to `Array<double,
2>::evaluate(SomeExpr<double, 1>&, Array<double, 2>::Select<false>)'
stest.cpp:21: error: candidates are: Array<T, N>& Array<T,
N>::evaluate(T_expr, Array<T, N>::Select<true>&) [with T_expr =
SomeExpr<double, 1>, T = double, int N = 2]
stest.cpp:28: error: Array<T, N>& Array<T,
N>::evaluate(T_expr, Array<T, N>::Select<false>&) [with T_expr =
SomeExpr<double, 1>, T = double, int N = 2]
and Intel (icc) 7.1 for linux:

stest.cpp(37): error: no instance of overloaded function "Array<T,
N>::evaluate [with T=double, N=2]" matches the argument list
argument types are: (SomeExpr<double, 2>, Array<double,
2>::Select<true>)
evaluate(expr,Select<ENUM_CAST(rank)==ENUM_CAST(T_ expr::rank)>());
^
detected during instantiation of "Array<T, N> &Array<T,
N>::evaluateExpr(T_expr) [with T=double, N=2, T_expr=SomeExpr<double, 2>]"

stest.cpp(37): error: no instance of overloaded function "Array<T,
N>::evaluate [with T=double, N=2]" matches the argument list
argument types are: (SomeExpr<double, 1>, Array<double,
2>::Select<false>)
evaluate(expr,Select<ENUM_CAST(rank)==ENUM_CAST(T_ expr::rank)>());
^
detected during instantiation of "Array<T, N> &Array<T,
N>::evaluateExpr(T_expr) [with T=double, N=2, T_expr=SomeExpr<double, 1>]"

compilation aborted for stest.cpp (code 2)

Jul 19 '05 #1
2 4176
Valeriu Catina wrote:
....
Array& evaluateExpr(T_expr expr)
{
return
evaluate(expr,Select<ENUM_CAST(rank)==ENUM_CAST(T_ expr::rank)>());


try :

evaluate<T>(expr,Select<ENUM_CAST(rank)==ENUM_CAST (T_expr::rank)>());

The problem is obviously in overload resolution and the standard is very
wordy about how and how not to resolve. I have yet to grok all of that
but I think that since your implicitly doing a conversion (from Select<>
to a Select<>&), it is not able to find a corresponding template to
resolve for.

Jul 19 '05 #2
Valeriu Catina wrote in news:bm**********@f40-3.zfn.uni-bremen.de:
Hi,

given the next code:
#define ENUM_CAST(x) int(x)

template<class T,int N>
class Array
{
public:
enum { rank = N };

protected:

// members ...

template<bool>
struct Select{ };

public:

template<class T_expr>
Array& evaluate(T_expr expr, Select<true>& )
Make this:

Array& evaluate(T_expr expr, Select<true> const & )

or as you've found out yourself:

Array& evaluate(T_expr expr, Select<true> )

[snip]
Array& evaluateExpr(T_expr expr)
{
return
Here you try to "bind" a temporary ( the Select<...>() ) to a
non-const reference, this is illegal in Standard C++, the compiler
that excepts it is probably operating in a msvc v6.0 compatibility
mode.
Which compiler is right ?


The one('s) that gave you the error.

HTH

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 19 '05 #3

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

Similar topics

6
2005
by: Dave | last post by:
Hello all, Consider this function template definition: template<typename T> void foo(T) {} If foo is never called, this template will never be instantiated. Now consider this explicit...
1
2255
by: Alfonso Morra | last post by:
if I have a class template declared as ff: (BTW is this a partial specialization? - I think it is) template <typename T1, myenum_1 e1=OK, my_enum_2=NONE> class A { public: A(); virtual...
10
2071
by: Suki | last post by:
Hi, I'm writing a templated class, and i dont want to use the class otherthan for some predetermined types, say, int, double etc. This class has no meaning for typenames other than those few. ...
12
2597
by: mlimber | last post by:
This is a repost (with slight modifications) from comp.lang.c++.moderated in an effort to get some response. I am using Loki's Factory as presented in _Modern C++ Design_ for message passing in...
1
2539
by: Leslaw Bieniasz | last post by:
Hello, I have the following problem: file a.h --------------- template <class T> class A { // some stuff
2
2298
by: pookiebearbottom | last post by:
Just trying to learn some things about templates. Was wondering how boost::tupple really works, but the headers were a bit confusing to me. I know you get do something like the following, just...
45
2841
by: charles.lobo | last post by:
Hi, I have recently begun using templates in C++ and have found it to be quite useful. However, hearing stories of code bloat and assorted problems I decided to write a couple of small programs...
2
2563
by: aitrob | last post by:
Hi, I have a problem concerning templates/inheritance. I have a code that compiles fine with g++ 4.0.1 (Apple version), but gives a lot of errors with Intel C++ 10.1 (Mac OS X). I'm not sure if...
4
3003
by: David Sanders | last post by:
Hi, I have a class with an integer template parameter, taking values 1, 2 or 3, and a function 'calc' in that class which performs calculations. Some calculations need only be performed if the...
3
1837
by: stdlib99 | last post by:
Hi, I have a simple question regarding templates and meta programming. I am going to try and work my way through the C++ Template Metaprogramming, a book by David Abrahams and Aleksey...
0
7412
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
7069
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
5652
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,...
1
5060
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4730
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3216
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
3203
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1570
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
441
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.