我只是尝试使用Scott Meyers在"嵌入式环境中的Effectice C++"中建议的一个放置新运算符.
DefaultMcuType::PortRegister* p = new(reinterpret_cast<void*>(0x05)) DefaultMcuType::PortRegister;
Run Code Online (Sandbox Code Playgroud)
然后我得到了以下错误:
register.cc: In function 'int main()':
register.cc:30:90: error: no matching function for call to 'operator new(sizetype, void*)'
DefaultMcuType::PortRegister* p = new(reinterpret_cast<void*>(0x05)) DefaultMcuType::PortRegister;
^~~~~~~~~~~~
<built-in>: note: candidate: void* operator new(unsigned int)
<built-in>: note: candidate expects 1 argument, 2 provided
<built-in>: note: candidate: void* operator new(unsigned int, std::align_val_t)
<built-in>: note: no known conversion for argument 2 from 'void*' to 'std::align_val_t'
register.cc:30:35: warning: unused variable 'p' [-Wunused-variable]
DefaultMcuType::PortRegister* p = new(reinterpret_cast<void*>(0x05)) DefaultMcuType::PortRegister;
^
Run Code Online (Sandbox Code Playgroud)
我真的无法弄清楚我做错了什么.
以下测试程序
import std;
int main() {}
Run Code Online (Sandbox Code Playgroud)
编译为gcc-14
/usr/local/bin/g++ -g -std=c++23 -fconcepts -fmodules-ts -O3 -Wall -Wextra test400.cc --output test400
Run Code Online (Sandbox Code Playgroud)
给出以下错误:
std: error: failed to read compiled module: No such file or directory
std: note: compiled module file is 'gcm.cache/std.gcm'
std: note: imports must be built before being imported
std: fatal error: returning to the gate for a mechanical issue
Run Code Online (Sandbox Code Playgroud)
标准库模块std应该是可用的,但显然不是。
std除了标头单元之外:是我自己创建模块的方法吗?
我想使用概念来区分一维和二维容器。我的第一次尝试如下:
template<typename C>
concept Container1D = requires(C c) {
std::begin(c);
std::end(c);
c[0];
};
template<typename C>
concept Container2D = requires(C c) {
std::begin(c);
std::end(c);
c[0, 0]; // interpreted as comma-operator
};
Run Code Online (Sandbox Code Playgroud)
但显然这不起作用,因为表达式0, 0被解释为逗号运算符,因此第二个概念也匹配一维容器。
有没有办法要求二维operator[a, b]?
template<typename T>
struct S {
using type = T;
};
volatile S<int> s;
template<typename T>
void f(T& v) {
using n = typename T::type;
S<n>::_; // to show
}
int main() {
f(s);
}
Run Code Online (Sandbox Code Playgroud)
在f 将T 被推断为volatile S<int>,但n仅仅是int.我有什么做的保护volatile,也就是说,有n是volatile int?
类型由位std::byte组成CHAR_BIT,并且可以多于8位。
那么,如何在 C++ 中声明真正的八位字节?
我使用类似以下最小例子的代码:
#include <stdint.h>
constexpr uint8_t size = 0; // sort of global config option
template<typename T = char, uint8_t L = size>
struct A {
T f() {
if constexpr(L > 0) {
return data[0];
}
return T{0};
}
int x = 0;
T data[L];
};
int main() {
A x;
x.f();
}
Run Code Online (Sandbox Code Playgroud)
现在我将编译器(g ++)设置更改为-pedantic,我收到以下警告:
ISO C++ forbids zero-size array [-Wpedantic]
这绝对没问题,但我想知道如何防止此消息?
以下代码使用 avolatile uint8_t而不是 avolatile sig_atomic_t作为 C 标准要求,因为在 avr 平台上该类型sig_atomic_t不可用。
这仍然是合法的代码吗?使用合法吗__SIG_ATOMIC_TYPE__?
是否需要包含cli()/sei()宏?
#include <stdint.h>
#include <signal.h>
#include <avr/interrupt.h>
volatile uint8_t flag;
//volatile sig_atomic_t flag; // not available in avr-gcc
//volatile __SIG_ATOMIC_TYPE__ flag; // ok?
void isr() __asm__("__vector_5") __attribute__ ((__signal__, __used__, __externally_visible__));
void isr() {
flag = 1;
}
void func(void) {
for (uint8_t i=0; i<20; i++) {
flag = !flag;
}
}
Run Code Online (Sandbox Code Playgroud)