474,034 Members | 3,887 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<WIDTHope rator *(
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<1transla tion;

translation = translation * transform;

return 0;
}

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

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<WIDTHope rator *(
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<1transla tion;

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 "translatio n * 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 "translatio n * 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 "translatio n * 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<1transfo rm;
Vector<1transla tion;

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++.m oderated 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
2618
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 the code below. The function template < typename T >
6
2217
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
2492
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> registrar; }; The constructor of Registrar does the registering when it is initialized.
10
2123
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. ========================= template <class T> myClass {....} ;
12
2643
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 an embedded environment with multiple processors. I created a policy for classes, which, I had hoped, would automatically register the class with the appropriate factory: // In some header file... #include <cassert>
2
2040
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. This makes it difficult to spot errors. For example, F4 to "jump to next error" just jumps ot the next "template assistance" message. And since there are hundreds of them, this is quite obnoxious. Can I disable these things? Why is MSVC...
3
2978
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 'ple::imtx_impl<T>::at' //in my program: the derived class template <class T> class Matrix_adapter : public ple::imtx_impl<T{ protected:
5
1756
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
2159
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 cross-platform scripting engine. When the scripting engine is embedded in an application, a platform-specific support library is linked in.) My first attempt goes here: ---code begin (library)---
2
2620
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 I'm doing something wrong and g++ just doesn't notice, or if the people at Intel are doing something weird... What am I trying to do? I'm trying to implement a template class that inherits from the Blitz++ array library #include <blitz/array.h>...
0
10522
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
12118
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
11590
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
11969
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
11121
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
8682
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
5387
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 we have to send another system
2
4925
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3949
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.