www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Immutable cannot be casted to const when using pointers

reply "Jeroen Bollen" <jbinero gmail.com> writes:
For some reason this code doesn't work...

module app;

void main() {
	immutable char var = 'a';
	immutable(char)* varPtr = &var;
	test(&varPtr);
}

void test(immutable(char)** param) {
	test2(param);
}

void test2(const(char)** test2) {
	
}

It'll give you " Error: function app.test2 (const(char)** test2) 
is not callable using argument types (immutable(char)**)", which 
makes no sense as if a function requests a constant variable, 
immutable should suffice.

What's even weirder (although it is what should happen), if you 
remove one of the pointers, it compiles fine.

module app;

void main() {
	immutable char var = 'a';
	test(&var);
}

void test(immutable(char)* param) {
	test2(param);
}

void test2(const(char)* test2) {
	
}

Is this a bug? I'm using DMD.
Apr 08 2014
next sibling parent reply "John Colvin" <john.loughran.colvin gmail.com> writes:
On Tuesday, 8 April 2014 at 21:05:10 UTC, Jeroen Bollen wrote:
 For some reason this code doesn't work...

 module app;

 void main() {
 	immutable char var = 'a';
 	immutable(char)* varPtr = &var;
 	test(&varPtr);
 }

 void test(immutable(char)** param) {
 	test2(param);
 }

 void test2(const(char)** test2) {
 	
 }

 It'll give you " Error: function app.test2 (const(char)** 
 test2) is not callable using argument types 
 (immutable(char)**)", which makes no sense as if a function 
 requests a constant variable, immutable should suffice.

 What's even weirder (although it is what should happen), if you 
 remove one of the pointers, it compiles fine.

 module app;

 void main() {
 	immutable char var = 'a';
 	test(&var);
 }

 void test(immutable(char)* param) {
 	test2(param);
 }

 void test2(const(char)* test2) {
 	
 }

 Is this a bug? I'm using DMD.
Looks like a bug to me.
Apr 08 2014
parent reply "Jeroen Bollen" <jbinero gmail.com> writes:
On Tuesday, 8 April 2014 at 21:07:29 UTC, John Colvin wrote:
 On Tuesday, 8 April 2014 at 21:05:10 UTC, Jeroen Bollen wrote:
 For some reason this code doesn't work...

 module app;

 void main() {
 	immutable char var = 'a';
 	immutable(char)* varPtr = &var;
 	test(&varPtr);
 }

 void test(immutable(char)** param) {
 	test2(param);
 }

 void test2(const(char)** test2) {
 	
 }

 It'll give you " Error: function app.test2 (const(char)** 
 test2) is not callable using argument types 
 (immutable(char)**)", which makes no sense as if a function 
 requests a constant variable, immutable should suffice.

 What's even weirder (although it is what should happen), if 
 you remove one of the pointers, it compiles fine.

 module app;

 void main() {
 	immutable char var = 'a';
 	test(&var);
 }

 void test(immutable(char)* param) {
 	test2(param);
 }

 void test2(const(char)* test2) {
 	
 }

 Is this a bug? I'm using DMD.
Looks like a bug to me.
Alright, filing it.
Apr 08 2014
parent reply "Jeroen Bollen" <jbinero gmail.com> writes:
Here we go: https://d.puremagic.com/issues/show_bug.cgi?id=12549
Apr 08 2014
parent reply "Peter Alexander" <peter.alexander.au gmail.com> writes:
On Tuesday, 8 April 2014 at 21:13:47 UTC, Jeroen Bollen wrote:
 Here we go: https://d.puremagic.com/issues/show_bug.cgi?id=12549
Not a bug: What you are asking for is to allow immutable(T)** and int** to convert to const(T)** int a = 0; int* pa = &a; const(int)** ppa = &pa; // not currently allowed immutable(int) b = 1; *ppa = &b; // equivalent to pa = &b!!! i.e. pa (mutable) now points at b (immutable)!!!
Apr 08 2014
parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 04/08/2014 02:38 PM, Peter Alexander wrote:
 On Tuesday, 8 April 2014 at 21:13:47 UTC, Jeroen Bollen wrote:
 Here we go: https://d.puremagic.com/issues/show_bug.cgi?id=12549
Not a bug:
Yeah. A known issue in C and C++ as well: http://www.parashift.com/c++-faq/constptrptr-conversion.html Ali
Apr 08 2014
prev sibling next sibling parent Artur Skawina <art.08.09 gmail.com> writes:
On 04/08/14 23:05, Jeroen Bollen wrote:
 For some reason this code doesn't work...
 
 module app;
 
 void main() {
     immutable char var = 'a';
     immutable(char)* varPtr = &var;
     test(&varPtr);
 }
 
 void test(immutable(char)** param) {
     test2(param);
 }
 
 void test2(const(char)** test2) {
     
 }
 
 It'll give you " Error: function app.test2 (const(char)** test2) is not
callable using argument types (immutable(char)**)", which makes no sense as if
a function requests a constant variable, immutable should suffice.
void test2(const(char)** test2) { static char b = 'b'; *test2 = &b; } artur
Apr 08 2014
prev sibling parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Tue, 08 Apr 2014 17:05:09 -0400, Jeroen Bollen <jbinero gmail.com>  
wrote:

 For some reason this code doesn't work...

 module app;

 void main() {
 	immutable char var = 'a';
 	immutable(char)* varPtr = &var;
 	test(&varPtr);
 }

 void test(immutable(char)** param) {
 	test2(param);
 }

 void test2(const(char)** test2) {
 	
 }
...
 Is this a bug? I'm using DMD.
No, it's intended. If that was allowed, you could inadvertently overwrite immutable data without a cast. The rule of thumb is, 2 or more references deep does not implicitly convert. One reference deep is OK. -Steve
Apr 08 2014
parent reply "Jeroen Bollen" <jbinero gmail.com> writes:
On Tuesday, 8 April 2014 at 21:53:58 UTC, Steven Schveighoffer 
wrote:
 The rule of thumb is, 2 or more references deep does not 
 implicitly convert. One reference deep is OK.

 -Steve
For some reason I was expecting that to rhyme. :P Anyways thanks all, makes sense now. Are there any workarounds? The function taking the const int** is external(C) so I'm not sure what to do.
Apr 09 2014
parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Wed, 09 Apr 2014 06:25:34 -0400, Jeroen Bollen <jbinero gmail.com>  
wrote:

 On Tuesday, 8 April 2014 at 21:53:58 UTC, Steven Schveighoffer wrote:
 The rule of thumb is, 2 or more references deep does not implicitly  
 convert. One reference deep is OK.

 -Steve
For some reason I was expecting that to rhyme. :P Anyways thanks all, makes sense now. Are there any workarounds? The function taking the const int** is external(C) so I'm not sure what to do.
Without knowing more information, it's hard to say. You can create a temporary: // immutable(char)** v const(char)* x = *v; foo(&x); -Steve
Apr 10 2014