473,386 Members | 1,860 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,386 software developers and data experts.

Template instantiation problem

Here is a short snippet of code that does not compile (tested with Vc8
and Comeau). This is because somehow Vector<0gets instantiated, for
which the array size goes to 0. However, I don't quite get it why it
gets instantiated in the first place. So why does not this compile?

template <int N>
struct Vector
{
int data_[N];
};

template <int HEIGHT, int WIDTH>
struct Matrix
{
};

template <int HEIGHT, int WIDTH>
Vector<WIDTHoperator *(
const Vector<HEIGHT>&,
const Matrix<HEIGHT, WIDTH>&)
{
return Vector<WIDTH>();
}

template <int HEIGHT, int WIDTH>
Vector<WIDTH - 1operator *(
const Vector<HEIGHT - 1>&,
const Matrix<HEIGHT, WIDTH>&)
{
return Vector<WIDTH - 1>();
}

int main()
{
Matrix<1, 1transform;
Vector<1translation;

translation = translation * transform;

return 0;
}

--
Kalle Rutanen
http://kaba.hilvi.org
Sep 15 '06 #1
6 2408

Kaba wrote:
Here is a short snippet of code that does not compile (tested with Vc8
and Comeau). This is because somehow Vector<0gets instantiated, for
which the array size goes to 0. However, I don't quite get it why it
gets instantiated in the first place. So why does not this compile?

template <int N>
struct Vector
{
int data_[N];
};

template <int HEIGHT, int WIDTH>
struct Matrix
{
};

template <int HEIGHT, int WIDTH>
Vector<WIDTHoperator *(
const Vector<HEIGHT>&,
const Matrix<HEIGHT, WIDTH>&)
{
return Vector<WIDTH>();
}

template <int HEIGHT, int WIDTH>
Vector<WIDTH - 1operator *(
const Vector<HEIGHT - 1>&,
const Matrix<HEIGHT, WIDTH>&)
{
return Vector<WIDTH - 1>();
}

int main()
{
Matrix<1, 1transform;
Vector<1translation;

translation = translation * transform;

return 0;
}

--
Kalle Rutanen
http://kaba.hilvi.org

what do you think the following line instantiates from your given
example ?
return Vector<WIDTH - 1>();

Sep 15 '06 #2
what do you think the following line instantiates from your given
example ?
return Vector<WIDTH - 1>();
Right, but a template is only instantiated when used, and I am not using
that template function (but the other). The question is: why would that
function be instantiated?

--
Kalle Rutanen
http://kaba.hilvi.org
Sep 16 '06 #3

Kaba wrote:
what do you think the following line instantiates from your given
example ?
return Vector<WIDTH - 1>();

Right, but a template is only instantiated when used, and I am not using
that template function (but the other). The question is: why would that
function be instantiated?
Both operator*() functions are instantiated while the compiler is
resolving which function the expression "translation * transform" is to
call.

A simple fix would be to specialize Vector<0>:

template <>
struct Vector<0>
{
};

Greg

Sep 16 '06 #4
Both operator*() functions are instantiated while the compiler is
resolving which function the expression "translation * transform" is to
call.
Hmm.. Yes, sounds reasonable. So the 0-array declaration here does not
go under SFINAE?
A simple fix would be to specialize Vector<0>:

template <>
struct Vector<0>
{
};
Right.

--
Kalle Rutanen
http://kaba.hilvi.org
Sep 16 '06 #5
Kaba wrote:
Both operator*() functions are instantiated while the compiler is
resolving which function the expression "translation * transform" is to
call.

Hmm.. Yes, sounds reasonable. So the 0-array declaration here does not
go under SFINAE?
The failure is an instantiation failure that comes before the
substitution can even be attempted. Once vector<0is instantiable
(say, by providing a specialization) then the substitution does fail
without an error.

Greg

Sep 17 '06 #6
Hmm.. Yes, sounds reasonable. So the 0-array declaration here does not
go under SFINAE?

The failure is an instantiation failure that comes before the
substitution can even be attempted. Once vector<0is instantiable
(say, by providing a specialization) then the substitution does fail
without an error.
I am still not convinced. Let's take another try.. I simplified the
problem:

template <int N>
struct Vector
{
int data_[N];
};

template <int N>
struct Matrix
{
};

template <int N>
int operator *(
const Vector<N>&,
const Matrix<N>&)
{
return 0;
}

template <int N>
int operator *(
const Vector<N - 1>&,
const Matrix<N>&)
{
return 1;
}

int main()
{
Matrix<1transform;
Vector<1translation;

int result = translation * transform;

return 0;
}

If I were a compiler I would act like this:

1) If in the first function I take the Matrix<1to conclude N = 1, then
the other parameter is Vector<1and we have a match.

2) If in the first function I take the Vector<1to conclude N = 1, then
the other parameter is Matrix<1and we have a match.

3) If in the second function I take the Matrix<1to conclude N = 1,
then the other parameter is Vector<0and we don't have a match.

4) If in the second function I take the Vector<1to conclude N = 2,
then the other parameter is Matrix<2and we don't have a match.

Thus, without ever needing instantiation of any template, we have
concluded that the first function is the only possible candidate. Why
would the compiler need the instantiation of Vector<0or Matrix<2>?

Btw, I reposted this question to comp.lang.c++.moderated before you
replied this second time. I thought there will be no more replies
because this newsgroup has such a traffic all the time. The comment I
make there is written before you replied, so it is not to say that you
couldn't deliver the answer.

Anyway, you still haven't convinced me of the reasons which lead to the
instantion of Vector<0>.. Examples showing its necessity and/or relevant
quotes from the standard would help.

--
Kalle Rutanen
http://kaba.hilvi.org
Sep 18 '06 #7

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

Similar topics

4
by: C. Carbonera | last post by:
/* Hi, I have a problem with explicit instantiation of templates in Visual C++ 6.0. I have provided the source below. I have an example of a function template that produces incorrect output in...
6
by: Dmitry Epstein | last post by:
Here is an example of a problem, which I tried to reduce to its bare essentials: // begin test1.cpp class E { public: template<class T> static void f(); }; template<class T> void E::f() {}
7
by: Drew McCormack | last post by:
I have a C++ template class which contains a static variable whose construction registers the class with a map. Something like this: template <typename T> class M { static Registrar<M>...
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...
2
by: Rudy Ray Moore | last post by:
Whenever I get any error with Vc++7.1/.net/2003, it is followed by huge ammounts of "template assistance" error messaging referencing template code (MTL) that has nothing to do with the error. ...
3
by: erictham115 | last post by:
Error C2555 c:\C++ projects\stat1\stdmatrix_adapt.h(41) : error C2555: 'std_tools::Matrix_adapter<T>::at': overriding virtual function return type differs and is not covariant from...
5
by: lobequadrat | last post by:
Hello, I am trying to get the following code work (unfortunately not mine ... :( ) template <class Tclass Test { public: class ELEM;
8
by: Ole Nielsby | last post by:
I want to create (with new) and delete a forward declared class. (I'll call them Zorgs here - the real-life Zorks are platform-dependent objects (mutexes, timestamps etc.) used by a...
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...
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: 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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...

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.