public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Invalid use of 'void'
@ 2020-05-18 10:30 Игорь Горлов
  2020-05-18 15:03 ` Niklas Gürtler
  0 siblings, 1 reply; 6+ messages in thread
From: Игорь Горлов @ 2020-05-18 10:30 UTC (permalink / raw)
  To: gcc-help

Hi all.

I have an issue when compiling a simple console program on Windows (Msys2).
First, let's consider the program itself:

void.cpp:
#include <cstddef>
#include <cstdlib>
#include <iostream>
int main()
{
// Getting any 2 valid void pointers
void* p1=std::malloc(32);
void* p2=std::malloc(64);
if(p1==nullptr||p2==nullptr)
{
std::cout << "Error: failed to get 2 valid void pointers" << std::endl;
return 1;
}
// Now trying to calculate the difference between these pointers
std::ptrdiff_t difference=p2-p1;
std::cout << "The difference is " << difference << std::endl;
return 0;
}

This program is not compatible with the C++17 standard. However, we can compile it with -std=gnu++17 (C++17 with GNU extensions), because one of that extensions permits calculations on void pointers. Additionally, we append -Wno-pointer-arith to suppress the related warning. So, our command line looks like this:

# g++ void.cpp -std=gnu++17 -Wno-pointer-arith -o void.exe

GCC responds:
void.cpp: In function 'int main()':
void.cpp:16:30: error: invalid use of 'void'
   16 | std::ptrdiff_t difference=p2-p1;
      |                              ^~

What am I doing wrong?

^ permalink raw reply	[flat|nested] 6+ messages in thread
* Invalid use of 'void'
@ 2020-05-19 11:24 Игорь Горлов
  0 siblings, 0 replies; 6+ messages in thread
From: Игорь Горлов @ 2020-05-19 11:24 UTC (permalink / raw)
  To: gcc-help

Thank you for the valuable information, Niklas and Jonathan. I made my code to work by explicit conversions from void* to std::uintptr_t.

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2020-05-19 11:32 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-05-18 10:30 Invalid use of 'void' Игорь Горлов
2020-05-18 15:03 ` Niklas Gürtler
2020-05-18 20:03   ` Jonathan Wakely
2020-05-19 11:07     ` Florian Weimer
2020-05-19 11:31       ` Jonathan Wakely
2020-05-19 11:24 Игорь Горлов

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).