472,353 Members | 1,643 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

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 3962
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
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...
1
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,...
10
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...
12
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...
1
by: Leslaw Bieniasz | last post by:
Hello, I have the following problem: file a.h --------------- template <class T> class A { // some stuff
2
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...
45
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...
2
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...
4
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...
3
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...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

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.