473,405 Members | 2,279 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,405 software developers and data experts.

constructor specialization

Hi!

I have a class like:

template<int size_integer, int size_fraction>
class number {

template<typename charT, typename traitsT>
explicit number(const charT* textual, int radix = 10);

template<typename charT>
explicit number<charT, std::char_traits<charT> >(const charT* textual,
int radix = 10); // line 25

}; // line 47

My compiler (MS VS .NET 2005) claims on the second declaration.

j:\number\number.hpp(25) : error C2975: 'number_library::number' : invalid
templ
ate argument for 'size_integer', compile-time evaluatable constant
expression ex
pected
j:\number\number.hpp(47) : see declaration of
'number_library::number'
j:\number\number.hpp(47) : see reference to class template
instantiation
'number_library::number<size_integer,size_fraction >' being compiled

So it is clear that the compiler understends it as a class specialization.
How to avoid it? How to specialzie constructor?

Perhabs this is totaly wrong thing I'm tring to do (I do not have much
experinece with templates). What I want to achive is to have a constructor
which
a) can construct number form textual representation (obviously)
b) do not have to write to versions (for char and wchar_t)
c) allow users to use their types and traits
d) allow simple constructions like

number<10, 10> big = "1000000000000000000000000000000";

But this does not work if I have only one constructor parametrized with
char type and traits type becauce traits type cannot be deducted by the
compiler. My compiler (or the language itself?) does not allow me to have
default parameters here in function. And I don't want to write two versions:
for default traits and parmetrized.

What and how (and why) to do?

Thanks in advance.

Adam Badura

P.S. It is not my homework. I do it for fun and exercise.
Apr 27 '06 #1
6 2559
And the next question is how to make MAX or MIN in template
specialization.

template<int size_integer1T, int size_fraction1T, int size_integer2T, int
size_fraction2T>
number< (size_integer1T > size_integer2T ? size_integer1T :
size_integer2T),
(size_fraction1T > size_fraction2T ? size_fraction1T :
size_fraction2T) >
operator +(const number<size_integer1T, size_fraction1T>& v1, const
number<size_integer1T, size_fraction1T>& v2);

I want the global operator + to return typ capable of stroing the hole
result. So I need to MAX of the fraction and integer parts. operator ?: does
not work... What to do?

Adam Badura
Apr 27 '06 #2
> template<int size_integer1T, int size_fraction1T, int size_integer2T, int
size_fraction2T>
number< (size_integer1T > size_integer2T ? size_integer1T :
size_integer2T),
(size_fraction1T > size_fraction2T ? size_fraction1T :
size_fraction2T) >
operator +(const number<size_integer1T, size_fraction1T>& v1, const
number<size_integer1T, size_fraction1T>& v2);


Naturally the second parameter of the operaotr should be
number<size_integer2T, size_fraction2T>
instead of
number<size_integer1T, size_fraction1T>

Sorry for that mistake. But now not only I have a bad code, but the
compiler (MS VS .NET 2005) ends with an intenral error... :)

Adam Badura
Apr 27 '06 #3
Adam Badura wrote:
I have a class like:

template<int size_integer, int size_fraction>
class number {

template<typename charT, typename traitsT>
explicit number(const charT* textual, int radix = 10);

template<typename charT>
explicit number<charT, std::char_traits<charT> >(const charT*
textual, int radix = 10); // line 25

}; // line 47

My compiler (MS VS .NET 2005) claims on the second declaration.

j:\number\number.hpp(25) : error C2975: 'number_library::number' :
invalid templ
ate argument for 'size_integer', compile-time evaluatable constant
expression ex
pected
j:\number\number.hpp(47) : see declaration of
'number_library::number'
j:\number\number.hpp(47) : see reference to class template
instantiation
'number_library::number<size_integer,size_fraction >' being compiled

So it is clear that the compiler understends it as a class
specialization. How to avoid it? How to specialzie constructor?
(a) There are no partial specialisations of functions

(b) You cannot specialise a member inside the class definition (IIRC)

(c) You don't need to specialise your constructor template, simply
overload it.
Perhabs this is totaly wrong thing I'm tring to do (I do not have
much experinece with templates). What I want to achive is to have a
constructor which
a) can construct number form textual representation (obviously)
b) do not have to write to versions (for char and wchar_t)
c) allow users to use their types and traits
d) allow simple constructions like

number<10, 10> big = "1000000000000000000000000000000";
....and that's why you declared your c-tors "explicit"?... Never mind.
With explicit c-tors you only can do

number<10,10> big("1000000000000000000000000000000");
But this does not work if I have only one constructor parametrized
with char type and traits type becauce traits type cannot be deducted
by the compiler. My compiler (or the language itself?) does not allow
me to have default parameters here in function.
Huh?
And I don't want to
write two versions: for default traits and parmetrized.

What and how (and why) to do?


template<int size_integer, int size_fraction>
class number {

// how do you suppose to use 'traitsT' here:
template<typename charT, typename traitsT>
explicit number(const charT* textual, int radix = 10);
// ??

template<typename charT>
explicit number(const charT* textual, int radix = 10);
};

The user of your type will not be able to supply traitsT. In order to
be able to recognize those you need to give your constructor an argument
that would help the compiler to deduce 'traitsT'.

Another way is to make your 'traitsT' an argument of the class template
and _there_ you will be able to supply default argument:
template<int size_integer, int size_fraction,
template<class> traitsT = std::char_traits >
class number {
template<typename charT>
explicit number(const charT* textual, int radix = 10);
// use 'traitsT' somehow...
};

Now, the user will be able to say

number<10,10,mytraits> big("blahblahblahblah",36);

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 27 '06 #4
Adam Badura wrote:
And the next question is how to make MAX or MIN in template
specialization.

template<int size_integer1T, int size_fraction1T, int size_integer2T,
int size_fraction2T>
number< (size_integer1T > size_integer2T ? size_integer1T :
size_integer2T),
(size_fraction1T > size_fraction2T ? size_fraction1T
: size_fraction2T) >
operator +(const number<size_integer1T, size_fraction1T>& v1, const
number<size_integer1T, size_fraction1T>& v2);

I want the global operator + to return typ capable of stroing the
hole result. So I need to MAX of the fraction and integer parts.
operator ?: does not work... What to do?


You need a template MAX and MIN:

template<int a, int b> struct MAX { enum { value = (a > b ? a : b) }; };
template<int a, int b> struct MIN { enum { value = (a < b ? a : b) }; };

template<int i> struct T { int foo() { return i;} };
template<int j, int k> T<MAX<j,k>::value> operator +(T<j>, T<k>) {
return T<MAX<j,k>::value>();
}
int main() {
T<10> t10;
T<20> t20;
return (t10+t20).foo();
}

V

--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 27 '06 #5
> You need a template MAX and MIN:

template<int a, int b> struct MAX { enum { value = (a > b ? a :
b) }; };
template<int a, int b> struct MIN { enum { value = (a < b ? a :
b) }; };


Thx.

Adam Badura
Apr 28 '06 #6
> (c) You don't need to specialise your constructor template, simply
overload it.
Oh... Right. Good point. My mistake not to think about it.
number<10, 10> big = "1000000000000000000000000000000";


...and that's why you declared your c-tors "explicit"?... Never mind.
With explicit c-tors you only can do

number<10,10> big("1000000000000000000000000000000");


Are not this constructs equal? I thought both use constructor....
template<int size_integer, int size_fraction>
class number {

// how do you suppose to use 'traitsT' here:
template<typename charT, typename traitsT>
explicit number(const charT* textual, int radix = 10);
// ??

template<typename charT>
explicit number(const charT* textual, int radix = 10);
};
Good point. I thought about removing them as useless. But then I was not
sure if i would not require them while parsing for white spaces for example.
And I just made it to chcecif I can and how to do it (as I wrote it is not
homework, project or something like this - I do it for fun and excercise)
The user of your type will not be able to supply traitsT. In order to
be able to recognize those you need to give your constructor an argument
that would help the compiler to deduce 'traitsT'.
Will not be able? Will not he/she be able to explicitly specialize
constructor with types he wants?
Another way is to make your 'traitsT' an argument of the class template
and _there_ you will be able to supply default argument:


This is possibel but seems "strange". This is just a constructor to ease
life, and char traits do not have much in common wiht number type.

Adam Badura
Apr 28 '06 #7

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

Similar topics

8
by: Agent Mulder | last post by:
Hi group, I have a problem with partial template specialization. In the code below I have a template struct Music with one method, play(), and three kinds of music, Jazz, Funk and Bach. When I...
8
by: daniel.w.gelder | last post by:
Hello, I have a template with two typenames. template <typename BASE, typename SUPER> struct Morphic { Morphic<BASE,SUPER>( ) { .. } ~Morphic<BASE,SUPER>( ) { .. } }; Morphic takes a Base...
4
by: Matthias Spiller | last post by:
Hi, following problem. I'm writing an Image class and want to use templates: .... template<class TType, int TLayers> class Image { public: Image();
19
by: Nicolas Fleury | last post by:
Hi everyone, I would to know what do you think of this PEP. Any comment welcomed (even about English mistakes). PEP: XXX Title: Specialization Syntax Version: $Revision: 1.10 $...
9
by: muzmail | last post by:
I have declared a copy constructor for a template class in a Visual C++ project but for some reason the compiler ignores it. I can put syntax errors in the copy constructor and the compiler ignores...
19
by: Jeroen | last post by:
Hi guys, I have a simple question. If I have a class like: class A { A(); ~A(); A(A& a); A(int i);
2
by: Barry | last post by:
The following code compiles with VC8 but fails to compiles with Comeau online, I locate the standard here: An explicit specialization of any of the following:
2
by: Kibiz0r | last post by:
Basically, I want a templated method to do one thing if the type T has a constructor that takes a std::istringstream, and another thing if it doesn't. The problem is that if I put the...
8
by: flopbucket | last post by:
Hi, I want to provide a specialization of a class for any type T that is a std::map. template<typename T> class Foo { // ... };
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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,...
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
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,...

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.