473,383 Members | 1,953 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 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 4152
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 be instantiated. Now consider this explicit...
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, my_enum_2=NONE> class A { public: A(); virtual...
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 no meaning for typenames other than those few. ...
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 presented in _Modern C++ Design_ for message passing in...
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 you get do something like the following, just...
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 problems I decided to write a couple of small programs...
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 with Intel C++ 10.1 (Mac OS X). I'm not sure if...
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 calculations. Some calculations need only be performed if the...
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 Metaprogramming, a book by David Abrahams and Aleksey...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.