473,569 Members | 2,490 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Template and default values of non-template pointers arguments

Hi,

what i'm trying to do is:

/////////////// Code Start

template <class TType, int* p = 0>
class Template
{
public:
Template<TType, p>() {};

};

/////////////// Code End

That works on Visual Studio 2005, but doesn't work on mingw 5.1.4 and
comeau.

They both says:

mingw:
could not convert template argument `0' to `int*

comeau:
argument of type "int" is incompatible with template parameter of type
"int *"
template <class TType, int* p = 0>
^

so the problem is quite clear, it seems there's no an acceptable
conversion from int to int*

But if i try to do something like that:

/////////////// Code Start

#define zero (int*)0

template <class TType, int* p = zero>
class Template
{
public:
Template<TType, p>() {};

};

/////////////// Code End
Now, that works on comeau, but still doesn't work with mingw.
My questions:
1] Why is that illegal?
2] How can solve this problem?
Many thanks,

--

Clyde
Oct 8 '08 #1
2 6611
Clyde wrote:
what i'm trying to do is:

/////////////// Code Start

template <class TType, int* p = 0>
class Template
{
public:
Template<TType, p>() {};

};

/////////////// Code End
No, that's not what you're trying to do. That's *how* you're trying to
do what you think you need. You should consider explaining *what* you
think you need the null pointer for. There can be no compile-time check
for a null pointer...
>
That works on Visual Studio 2005, but doesn't work on mingw 5.1.4 and
comeau.
It must be an extension offered by VC++.
>
They both says:

mingw:
could not convert template argument `0' to `int*

comeau:
argument of type "int" is incompatible with template parameter of type
"int *"
template <class TType, int* p = 0>
^

so the problem is quite clear, it seems there's no an acceptable
conversion from int to int*

But if i try to do something like that:

/////////////// Code Start

#define zero (int*)0

template <class TType, int* p = zero>
class Template
{
public:
Template<TType, p>() {};

};

/////////////// Code End
Now, that works on comeau, but still doesn't work with mingw.
My questions:
1] Why is that illegal?
Here we just say "because the Standard says so". 14.3.2/5 prohibits '0'
(the integer literal) to be used as the non-type template *argument* for
a template *parameter* of type 'pointer to T'. The conversions do not
apply.

If you need the real rationale, you'll have to to ask in 'comp.std.c++'
when it's operational.
2] How can solve this problem?
Solve? Not sure what you mean. The requirement is that the pointer
non-type template argument to be the address of a real object, with
external linkage. A null pointer does not satisfy that requirement.
Now, if you do have a problem (that you're trying to solve by writing
your template with the wrong default argument), you haven't stated it
yet, so we can't really tell you how to solve it. Please explain what
you're trying to accomplish.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 8 '08 #2
On Oct 9, 1:00 am, Victor Bazarov <v.Abaza...@com Acast.netwrote:
Clyde wrote:
what i'm trying to do is:
/////////////// Code Start
template <class TType, int* p = 0>
class Template
{
public:
Template<TType, p>() {};
};
/////////////// Code End
No, that's not what you're trying to do. That's *how* you're
trying to do what you think you need. You should consider
explaining *what* you think you need the null pointer for.
There can be no compile-time check for a null pointer...
Surely you've heard of partial specialization. Or template
meta-programming (things like boost::enable_i f). Or perhaps he
doesn't need a compile-time check; just somewhere in the code,
he has "if ( p != NULL ) ...". (Of course, since p is in fact a
constant in any particular instantiation, the compiler can
evaluate the if at compile time, and not generate the body if
the pointer is null. However...)
That works on Visual Studio 2005, but doesn't work on mingw
5.1.4 and comeau.
It must be an extension offered by VC++.
Note that there are really two separate issues here. First is
the fact that the standard does not apply the null pointer
constant conversion, so 0 can only be used to instantiate a
template argument of integral type. The second is that the
standard doesn't allow a null pointer, period, as the argument
to a non-type template parameter of pointer type---the argument
must be "the address of an object or function with external
linkage, including function templates and function
template-ids[...]". A null pointer is NOT the address of an
object, so it's not allowed.

The next release of the standard will allow null pointers,
although it won't allow the use of 0 for them; the argument
would have to be either (int*)0 or nullptr.
They both says:
mingw:
could not convert template argument `0' to `int*
comeau:
argument of type "int" is incompatible with template
parameter of type "int *"
template <class TType, int* p = 0>
so the problem is quite clear, it seems there's no an
acceptable conversion from int to int*
There is no implicit conversion from int to int*, ever. The
conversion in question here is the "null pointer constant"
conversion: a null pointer constant (an constant integral
expression evaluating to zero) will convert implicitly to a
pointer in some specific contexts. According to the standard,
this is not one of them.
But if i try to do something like that:
/////////////// Code Start
#define zero (int*)0
template <class TType, int* p = zero>
class Template
{
public:
Template<TType, p>() {};
};
/////////////// Code End
Now, that works on comeau, but still doesn't work with mingw.
It's illegal, at least according to C++03. It will be legal in
the next version of the standard.
My questions:
1] Why is that illegal?
Here we just say "because the Standard says so". 14.3.2/5
prohibits '0' (the integer literal) to be used as the non-type
template *argument* for a template *parameter* of type
'pointer to T'. The conversions do not apply.
If you need the real rationale, you'll have to to ask in
'comp.std.c++' when it's operational.
Historically, *no* implicit conversions were allowed for
non-type template arguments; IIRC, the first implementation of
templates I used wouldn't accept 0 for a long argument. (It had
to be 0L. And I'll admit that this was a long time ago, and my
memory isn't that sure.) Basically, templates were something
new, and without any experience, it wasn't too clear what the
implications of conversions might be. In the standard, integral
promotions and integral conversions are allowed (so 0 can be
used for an argument of type long), but the only conversions
allowed for a pointer argument are array to pointer (so you can
use the name of an array) and adding cv qualifications (so you
can pass the address of a non-const object to a pointer to
const). Not even derived to base is applied (implicitly).

The next version of the standard will loosen this requirement
somewhat, and allow conversions of std::nullptr_t to a pointer
type.
2] How can solve this problem?
Solve? Not sure what you mean. The requirement is that the
pointer non-type template argument to be the address of a real
object, with external linkage. A null pointer does not
satisfy that requirement.
Yes, but that requirement will be loosened. In the meantime...
the solution to every problem is an additional level of
indirection, as they used to say. He can definitely do
something like:

extern int *const null = 0 ;

template< typename TType, int *const& p = null >
...

Of course, every time he wants to instantiate the template,
he'll have to introduce an additional variable ,so the cure may
be worse than the disease. (Note too that the "extern" is
necessary in the above. By default, const objects have internal
linkage, and a template can only be instantiated over objects
with external linkage.) Alternatively, he just provides a dummy
int, and uses its address as the default; within the template,
he can check if the argument is equal to the dummy address.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Oct 9 '08 #3

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

Similar topics

18
7811
by: Dan Cernat | last post by:
Hi there, A few threads I had a little chat about default values. I am starting this thread because I want to hear more opinions about the default values of function parameters. Some say they see no use of them. Others say thar they are bad. I like them. So, could anyone tell me why they are in the standard? Are they bad? I do not intend...
10
13715
by: sam | last post by:
Hi, I m wondering why I can't declare reference variable for default value in a function argument variable? eg. class A { void f(string &str="");
10
6951
by: serge | last post by:
I am doing a little research on Google about this topic and I ran into this thread: http://groups.google.com/group/microsoft.public.sqlserver.programming/browse_thread/thread/63cce060ff152dcc/1dc13d4ee6758966?lnk=st&q=difference+constraint+sql+defaults&rnum=14#1dc13d4ee6758966 I read SQL Server MVP Louis Davidson's post saying: "Actually...
5
3216
by: Mike Nolan | last post by:
I notice that 7.4 doesn't do default ordering on a 'group by', so you have to throw in an 'order by' clause to get the output in ascending group order. Is this something that most RDB's have historically done (including PG prior to 7.4) but isn't really part of the SQL standard? On a mostly unrelated topic, does the SQL standard indicate...
8
3494
by: Bern McCarty | last post by:
Is it at all possible to leverage mixed-mode assemblies from AppDomains other than the default AppDomain? Is there any means at all of doing this? Mixed-mode is incredibly convenient, but if I cannot load/unload/reload extensions into my large and slow-to-load application during development without restarting the process then the...
10
8529
by: utab | last post by:
Dear all, Can somebody direct me to some resources on the subject or explain the details in brief? I checked the FAQ but could not find or maybe missed. Regards,
3
4748
by: Charlie | last post by:
Imagine I subclass Panel into MyPanel and set a few property values to my own defaults as shown below. Eg. I set BackColor to by LightYellow. When I add MyPanel to a form, it will write the non-default property values into the InitializeComponent of the form. So it will explicitily set myPanel1.BackColor = Color.LightYellow. If I later...
23
3634
by: Jess | last post by:
Hello, I understand the default-initialization happens if we don't initialize an object explicitly. I think for an object of a class type, the value is determined by the constructor, and for the built-in types, the value is usually garbage. Is this right? However, I'm a bit confused about value-initialization, when does it happen, and...
4
17257
by: Dave Burns | last post by:
Hello, I am trying to specify a logical default value for a in a WCF Web Service using basicHttpBinding. I realize that the language defaults are: int - 0 string - null bool - false
43
3188
by: kenneth | last post by:
Dear all, I have encountered this weird problem. I have a class definition with an __init__ argument 'd' which defaults to {}. This argument is put in the 'self.d' attribute at initialization I create two independent instances of this class; the code is as follows.
0
7618
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7926
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. ...
0
8132
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...
1
5514
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...
0
5222
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3656
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3644
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1226
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
944
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...

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.