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

problem with simulating typedef templates

I am trying to simulate typedef template similar to the suggestion of
Herb Sutter in the following article: http://www.gotw.ca/gotw/079.htm

However when implementing typedef templates according to his
suggestion, I run into type inference problems when passing typedef
templates as arguments to template functions. It seems the compiler
(gcc 4.0) cannot resolve the typedef template with an instantiation of
that typedef template. Below is an example to illustrate the exact
problem, along with the corresponding compiler error message. I would
like to the compiler to pattern match LinAlTraits<double>::Vector2 in
the function testThis, but instead it is trying to use the underlying
type boost::numeric::ublas::c_vector<double,2>. Any suggestions on a
workaround or idiom that would allow the syntactic nicety shown below
would be greatly appreciated.

#include <boost/numeric/ublas/vector.hpp>

template <typename Real>
struct LinAlTraits
{
typedef boost::numeric::ublas::c_vector<Real,2> Vector2;
};

template<typename T>
void
testThis(const typename LinAlTraits<T>::Vector2& v2)
{

}

int
main()
{
LinAlTraits<double>::Vector2 v2;
testThis(v2);
}

Here is the error message returned by gcc...

main.cc: In function 'int main()':
main.cc:20: error: no matching function for call to
'testThis(boost::numeric::ublas::c_vector<double, 2u>&)'

"g++" -Wall -ftemplate-depth-100 -O0 -fno-inline -g -c -o
"bin/gcc/debug/main.o" "main.cc"
Thanks,
Jimmy.

Nov 29 '05 #1
5 2414
jimmy wrote:
I am trying to simulate typedef template similar to the suggestion of
Herb Sutter in the following article: http://www.gotw.ca/gotw/079.htm

However when implementing typedef templates according to his
suggestion, I run into type inference problems when passing typedef
templates as arguments to template functions. It seems the compiler
(gcc 4.0) cannot resolve the typedef template with an instantiation of
that typedef template. Below is an example to illustrate the exact
problem, along with the corresponding compiler error message. I would
like to the compiler to pattern match LinAlTraits<double>::Vector2 in
the function testThis, but instead it is trying to use the underlying
type boost::numeric::ublas::c_vector<double,2>. Any suggestions on a
workaround or idiom that would allow the syntactic nicety shown below
would be greatly appreciated.

#include <boost/numeric/ublas/vector.hpp>

template <typename Real>
struct LinAlTraits
{
typedef boost::numeric::ublas::c_vector<Real,2> Vector2;
};

template<typename T>
void
testThis(const typename LinAlTraits<T>::Vector2& v2)
{

}

int
main()
{
LinAlTraits<double>::Vector2 v2;
testThis(v2);
}

Here is the error message returned by gcc...

main.cc: In function 'int main()':
main.cc:20: error: no matching function for call to
'testThis(boost::numeric::ublas::c_vector<double, 2u>&)'

"g++" -Wall -ftemplate-depth-100 -O0 -fno-inline -g -c -o
"bin/gcc/debug/main.o" "main.cc"
Thanks,
Jimmy.


Right, C++ is limiteed in the parameter forms that can be deduced in a
template function. In particular if you write

template <T>
void func(typename A<T>::B p, ...

then the parameter p wil not take place in template argument deduction.

Usual workaround is to use iterators, in your case you could drop the
template typedef for testThis.

john
Nov 29 '05 #2
jimmy wrote:
I am trying to simulate typedef template similar to the suggestion of
Herb Sutter in the following article: http://www.gotw.ca/gotw/079.htm

However when implementing typedef templates according to his
suggestion, I run into type inference problems when passing typedef
templates as arguments to template functions. It seems the compiler
(gcc 4.0) cannot resolve the typedef template with an instantiation of
that typedef template. Below is an example to illustrate the exact
problem, along with the corresponding compiler error message. I would
like to the compiler to pattern match LinAlTraits<double>::Vector2 in
the function testThis, but instead it is trying to use the underlying
type boost::numeric::ublas::c_vector<double,2>. Any suggestions on a
workaround or idiom that would allow the syntactic nicety shown below
would be greatly appreciated.

#include <boost/numeric/ublas/vector.hpp>

template <typename Real>
struct LinAlTraits
{
typedef boost::numeric::ublas::c_vector<Real,2> Vector2;
};

template<typename T>
void
testThis(const typename LinAlTraits<T>::Vector2& v2)
{

}

int
main()
{
LinAlTraits<double>::Vector2 v2;
testThis(v2);
}


The compiler can't automatically deduce that template's paramter. This
is the standard.

What's wrong with:

template<typename T>
void
testThis(const T& v2)
{
typedef typename T::value_type TReal;
//I'm not sure that boost/numeric/ublas/vector.hpp
//has a "value_type" but I suspect that it would.
}

Ok, so you want overloads as well.

This technique below is sometimes useful. When matching for template
functions, if there is an error when evaluating one of the template
function paramter types, that function is excluded from the search. Not
all compilers know how to do this properly yet. gcc 4 does, msvc7.1
ICE's sometimes.

#include <vector>

template <typename Real>
struct LinAlTraits
{
typedef std::vector<Real> Vector2;
};

template <typename T1, typename T2>
struct IsSame
{
};

template <typename T>
struct IsSame<T,T>
{
typedef int type;
};
template<typename T>
void
testThis(
const T & v2,
typename IsSame<T,typename LinAlTraits<typename
T::value_type>::Vector2>::type dummy = 0
)
{

}

int
main()
{
LinAlTraits<double>::Vector2 v2;

testThis(v2);

int vx;

testThis(vx);
}

Nov 29 '05 #3
Thanks you for the responses. This inability to do this type of lookup
seems peculiar. If you are going to use the metafunction IsSame to
test if two types are the same, why can't the compiler do this
automatically? Can you point me to the standard, or maybe an
explanation of why this cannot be done. I could say this and this
would work...

boost::numeric::ublas::c_vector<Real,2> v1;
LinAlTraits<double>::Vector2 v2;
v2 = v1;

Also what about return types? Could I do something like this... I
think so.

template<typename Real>
typename LinAlTraits<Real>::Vector2
anotherTest(const boost::numeric::ublas::c_vector<Real,2>& v3)
{
return v3;
}

....

int main()
{
LinAlTraits<double>::Vector2 v3;
boost::numeric::ublas::c_vector<double,2> v4 = anotherTest(v3);
return 0;
}

I'm confused as to what type inferences the compiler can and cannot
make.

Thanks,
Jimmy.

Nov 29 '05 #4
This should read...

boost::numeric::ublas::c_vector<doublel,2> v1;
LinAlTraits<double>::Vector2 v2;
v2 = v1;
jimmy wrote:
Thanks you for the responses. This inability to do this type of lookup
seems peculiar. If you are going to use the metafunction IsSame to
test if two types are the same, why can't the compiler do this
automatically? Can you point me to the standard, or maybe an
explanation of why this cannot be done. I could say this and this
would work...

boost::numeric::ublas::c_vector<Real,2> v1;
LinAlTraits<double>::Vector2 v2;
v2 = v1;

Also what about return types? Could I do something like this... I
think so.

template<typename Real>
typename LinAlTraits<Real>::Vector2
anotherTest(const boost::numeric::ublas::c_vector<Real,2>& v3)
{
return v3;
}

...

int main()
{
LinAlTraits<double>::Vector2 v3;
boost::numeric::ublas::c_vector<double,2> v4 = anotherTest(v3);
return 0;
}

I'm confused as to what type inferences the compiler can and cannot
make.

Thanks,
Jimmy.


Nov 29 '05 #5
jimmy wrote:
Thanks you for the responses. This inability to do this type of lookup
seems peculiar. If you are going to use the metafunction IsSame to
test if two types are the same, why can't the compiler do this
automatically? Can you point me to the standard, or maybe an
explanation of why this cannot be done. I could say this and this
would work...
I don't have a reference to the standard handy, I do remember asking the
same question a few years ago (possibly on c.l.c++ so you'll kikely find
it in the google archives.) and I do believe there was a reference to
the standard made at the time. It's got somthing to do about looking
into a class, since classes can be (partially) specialized, it's very
hard to know which class your're talking about, i.e. there may be
multiple template parameters that yield the same type, hence the lookup
search would need to be exhaustive which is prohibitive.

boost::numeric::ublas::c_vector<Real,2> v1;
LinAlTraits<double>::Vector2 v2;
v2 = v1;

Also what about return types? Could I do something like this... I
think so.

template<typename Real>
typename LinAlTraits<Real>::Vector2
anotherTest(const boost::numeric::ublas::c_vector<Real,2>& v3)
{
return v3;
}
Yes. Template parameters are not deduced by return type except for the
conversion operator.

...

int main()
{
LinAlTraits<double>::Vector2 v3;
boost::numeric::ublas::c_vector<double,2> v4 = anotherTest(v3);
return 0;
}

I'm confused as to what type inferences the compiler can and cannot
make.


It can't look inside a class. The example I gave you earlier, can be
spoofed by a judicious specialization, which may be bad or good
depending on what you're trying to do.

Nov 29 '05 #6

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

Similar topics

5
by: lomat | last post by:
Hello, While compiling a file, I get following error .... ================================= /usr/local/lib/gcc-lib/i686-pc-linux-gnu/2.95.3/include/g++/type_traits.h:14 2: redefinition of...
11
by: Alexander Stippler | last post by:
Hi, I have a little problem to design some template classes in a realizable way. I have some Container classes and some Proxy classes (proxies for elements). Looks like this: // P is the...
5
by: Michael Olea | last post by:
Here is a design problem I ran into this am - and I have cleaned the bathroom, scrubbed toilet sink and tub, windexed all glass, mopped the floor, and vacuumed the house - no dice, the problem is...
3
by: Generic Usenet Account | last post by:
This is a two-part question. (1) I have implemented a "Datastructure Registry" template class. I am getting no compiler warnings with older compilers, but newer compilers are generating the...
12
by: vvv | last post by:
Hi All, Do we have anything in .NET which is equivalent to C++'s Typedef . Regards, Vasanth
6
by: Hendrik Schober | last post by:
Hi, I have a problem with extending some existing code. In a simplified form, the problem looks like this: I have four types, A, B, C, and D. Each A refers to zero, one, or more B's and each...
4
by: Sacha | last post by:
I'm aware, that up to date, "typedef templates" are not defined within the C++ standard. The seemingly common workaround is this: template <class T> struct MyTypeDef { /* ultimately I need...
7
by: T.A. | last post by:
Class hierarchy below demonstrates my problem: #include <vector> #include <boost/smart_ptr.hpp> class Fruit { public: virtual ~Fruit() = 0; };
9
by: Jerome Durand | last post by:
Hello, I'm trying to write something along the following lines but I cannot get this to compile. template <typename derivedstruct Base { typedef typename derived::valueType valueType;...
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
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...
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
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...
0
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
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...
0
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,...
0
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...

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.