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

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 6578
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...@comAcast.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_if). 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 objektorientierter Datenverarbeitung
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
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...
10
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
by: serge | last post by:
I am doing a little research on Google about this topic and I ran into this thread: ...
5
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...
8
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...
10
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
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...
23
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...
4
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
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.