Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/152.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在C+;中是否可以将任意指针作为输入参数+;?_C++ - Fatal编程技术网

C++ 在C+;中是否可以将任意指针作为输入参数+;?

C++ 在C+;中是否可以将任意指针作为输入参数+;?,c++,C++,我想创建一个类来管理Cortex-M3设备的位带功能。对于那些不知道的人:处理器将特定区域内的每一位映射到一个完整的单词。这允许对特定位进行原子集操作。该类本身与std::uintptr\t一起工作。对于构造函数,我希望允许使用任意指针,因为我不在乎它指向什么。它可能是在设备头中定义的某个结构 我当前的实现为构造函数提供了: Bitband(uintptr_t address, uint32_t bitNumber); Bitband(void * ptr, uint32_t bitNumber

我想创建一个类来管理Cortex-M3设备的位带功能。对于那些不知道的人:处理器将特定区域内的每一位映射到一个完整的单词。这允许对特定位进行原子集操作。该类本身与std::uintptr\t一起工作。对于构造函数,我希望允许使用任意指针,因为我不在乎它指向什么。它可能是在设备头中定义的某个结构

我当前的实现为构造函数提供了:

Bitband(uintptr_t address, uint32_t bitNumber);
Bitband(void * ptr, uint32_t bitNumber);
我的应用程序调用构造函数的方式如下:

Bitband foo(reinterpret_cast<uintptr_t>(&gpioPort->IDR), pin);
如果我遗漏了reinterpret_cast,我会收到以下消息:

Src/led.cpp(5): error: no matching constructor for initialization of 'Bitband'
    ledOutputBitBand(&port->ODR, pin)
    ^                ~~~~~~~~~~~~~~~

./Inc/bitband.hpp(9): note: candidate constructor not viable: no known conversion from 'volatile uint16_t *' (aka 'volatile unsigned short *') to 'uintptr_t' (aka 'unsigned int') for 1st argument; remove &
  Bitband(uintptr_t address, uint32_t bitNumber);
  ^

./Inc/bitband.hpp(10): note: candidate constructor not viable: no known conversion from 'volatile uint16_t *' (aka 'volatile unsigned short *') to 'void *' for 1st argument
  Bitband(void * address, uint32_t bitNumber);
  ^

./Inc/bitband.hpp(6): note: candidate constructor (the implicit copy constructor) not viable: requires 1 argument, but 2 were provided
class Bitband
      ^

./Inc/bitband.hpp(6): note: candidate constructor (the implicit move constructor) not viable: requires 1 argument, but 2 were provided
1 error generated.

因此,我假设如果我想在另一个上下文中使用类Bitband,我必须再次使用reinterpret_cast?

鉴于您提供的要求,这是一种干净的方法

我真的不明白你打算调用哪个构造函数(你没有明确你的目标),但是,无论如何,
reinterpret\u cast
似乎最接近实际情况

它的冗长是为了提醒您应该重新审视您的设计,并提出一个一开始就不需要这种转换的设计。因为我们不再生活在20世纪70年代

但是,如果您坚持使用旧样式,您可以使用C样式转换:

Bitband foo((void*)&gpioPort->IDR, pin);

我恳求您不要这样做。

鉴于您提供的要求,这是一个干净的方法

我真的不明白你打算调用哪个构造函数(你没有明确你的目标),但是,无论如何,
reinterpret\u cast
似乎最接近实际情况

它的冗长是为了提醒您应该重新审视您的设计,并提出一个一开始就不需要这种转换的设计。因为我们不再生活在20世纪70年代

但是,如果您坚持使用旧样式,您可以使用C样式转换:

Bitband foo((void*)&gpioPort->IDR, pin);

我恳请您不要这样做。

&gpioPort->IDR
转换为
void*
(除非
IDR
const
)。请复制并粘贴整个错误消息以及导致错误消息的代码。我还没有尝试过这一点,但是如果将构造函数设置为一个模板,该模板使用指向模板类型的指针,然后在构造函数内部进行重新解释转换,会发生什么情况?如果您实际使用不同的指针类型调用构造函数,那么最终可能会扩大您的代码(如果它没有得到优化),但这可能会使您的调用看起来更干净。或者,您可以考虑使用宏来缩短重新解释CAST的词性。转换< <代码>和GPIOPTT > IDR>代码>到<代码> Value*/COD>(除非<代码> IDR <代码> const )应该没有问题。请复制并粘贴整个错误消息以及导致错误消息的代码。我还没有尝试过这一点,但是如果将构造函数设置为一个模板,该模板使用指向模板类型的指针,然后在构造函数内部进行重新解释转换,会发生什么情况?如果您实际使用不同的指针类型调用构造函数,那么最终可能会扩大您的代码(如果它没有得到优化),但这可能会使您的调用看起来更干净。或者,您可以考虑使用宏来缩短重新解释CAST的词性。
#include <led.hpp>
#include <cassert>

Led::Led(GPIO_TypeDef * const port, uint16_t const pin) :
    ledOutputBitBand(reinterpret_cast<uintptr_t>(&port->ODR), pin)
{
  assert(pin < 16);

  /* Set port mode to push pull */
  port->MODER |= 1 << ( 2 * pin);
}
Led greenLed(GPIOD, 0);
greenLed.Set(true);
Src/led.cpp(5): error: no matching constructor for initialization of 'Bitband'
    ledOutputBitBand(&port->ODR, pin)
    ^                ~~~~~~~~~~~~~~~

./Inc/bitband.hpp(9): note: candidate constructor not viable: no known conversion from 'volatile uint16_t *' (aka 'volatile unsigned short *') to 'uintptr_t' (aka 'unsigned int') for 1st argument; remove &
  Bitband(uintptr_t address, uint32_t bitNumber);
  ^

./Inc/bitband.hpp(10): note: candidate constructor not viable: no known conversion from 'volatile uint16_t *' (aka 'volatile unsigned short *') to 'void *' for 1st argument
  Bitband(void * address, uint32_t bitNumber);
  ^

./Inc/bitband.hpp(6): note: candidate constructor (the implicit copy constructor) not viable: requires 1 argument, but 2 were provided
class Bitband
      ^

./Inc/bitband.hpp(6): note: candidate constructor (the implicit move constructor) not viable: requires 1 argument, but 2 were provided
1 error generated.
Bitband foo((void*)&gpioPort->IDR, pin);