我试图找出发生在 MUD c 代码库上的错误的原因。其中 #define UMAX(a, b) ((a) > (b) ? (a) : (b)) 的使用用于返回两个值的最大值。它有时会返回较低的值,甚至调试我也找不到原因。
我隔离了复制的相关代码并在两个编译器中运行它,https ://www.tutorialspoint.com/compile_c_online.php (GCC) 上的在线 C 编译器和使用 Visual Studio 2019。两者都有以下代码吐出负值:
dur = UMAX(0, 4 - number_fuzzy(level / 10));
printf("Dur = %i\n", dur);
Run Code Online (Sandbox Code Playgroud)
基本上,UMAX 用于防止计算中出现非负值。我们将级别除以 10,然后使用模糊/随机函数将其随机更改为 -1/+0/+1。我们从 4 中减去这个值,如果该值小于零,我们使用 UMAX 返回 0。但是对于 50 到 59 级,这有时会产生负值,如下所示。但我从未见过它对 60-69 产生负面影响。我知道模糊随机是随机改变值,但 UMAX 守卫应该始终有效,而不仅仅是某些级别。
完整代码如下:
//==================================================================
//= SLAP ME ON https://www.tutorialspoint.com/compile_c_online.php =
//==================================================================
#include <stdio.h>
#include<time.h> // pd 5
#define UMAX(a, b) ((a) > (b) ? (a) : (b))
#define …Run Code Online (Sandbox Code Playgroud) 考虑以下代码:
public class DecimalWrapper
{
public static implicit operator DecimalWrapper(decimal x) => new();
}
[Fact]
public void Test()
{
// Why this even compiles? Since there is no implicit conversion from decimal? -> decimal
DecimalWrapper c = (decimal?)null; // variable 'c' is null!
}
Run Code Online (Sandbox Code Playgroud)
我根本不希望它编译,因为没有从十进制隐式转换?到十进制。
我认为这是一个错误还是我做错了什么?
我已经看到了这一点: 从 int 提升/可空转换的严重错误,允许从十进制转换
但这看起来不一样,而且已经过时了(7 年以上),所以现在应该修复错误,但无法确定,因为所有指向错误报告的链接都消失了)... :(
我真的很想在真正的解决方案中使用这样的代码(跟踪计算),但这阻止了我。
PS:我在 Windows 上编译。
几天前,我在 arch linux 上使用arm-none-eabi-gcc工具链时遇到了以下问题,它使用gcc13.2作为gcc版本,与我在Ubuntu中使用的工具链不同,我认为它会是某个版本所以我去了 gcc 发布页面阅读注释:https://gcc.gnu.org/gcc-13/changes.html,但是正如您所见,没有提到任何有关模板重新声明的内容
经过一些研究,我得出了这个可重现的例子:
// compiles fine under gcc 12.3
#include <concepts>
#include <type_traits>
template<typename T>
class Sensor{
public:
int read();
};
template<typename T>
requires std::is_integral_v<T>
int Sensor<T>::read(){
return 1;
}
int main(){
Sensor<int> s;
s.read();
}
//does not compile under gcc 13.2
#include <concepts>
#include <type_traits>
template<typename T>
class Sensor{
public:
int read();
};
template<typename T>
requires std::is_integral_v<T>
int Sensor<T>::read(){
return 1;
}
int main(){
Sensor<int> s;
s.read();
}
Run Code Online (Sandbox Code Playgroud)
有人可以解释为什么在 12.3 版本中允许,但在 13.2 中不允许吗?另外我认为 …
在过去的4个小时里,我一直在摸不着头脑,尝试各种小实验,但我似乎无法弄清楚出了什么问题.这可能是编译器错误吗?
Test.m:
- (id)initWithContentsOfURL:(NSURL *)aURL error:(NSError **)error
{
if (!(self = [super init])) {
return nil;
}
return self;
}
Run Code Online (Sandbox Code Playgroud)
main.m文件:
NSError *error;
Test *t = [[Test alloc] initWithContentsOfURL:[NSURL fileURLWithPath:@"/"] error:&error];
Run Code Online (Sandbox Code Playgroud)
这是编译器警告(来自main.m):
警告:不兼容的Objective-C类型'struct NSError**',当从'initWithContentsOfURL:error:'传递参数2时,期望'struct NSDictionary**'来自不同的Objective-C类型
我正在使用最新版本的Xcode和Snow Leopard.
我有以下计划
int a = 216;
bool* v = (bool*)((void*)&a);
std::cout << (*v == true) << endl;
Run Code Online (Sandbox Code Playgroud)
我希望这个程序打印出真或假,但它打印出216.我编译了它g++ (Ubuntu 4.8.2-19ubuntu1) 4.8.2.这是预期的行为还是一些错误?为什么等式运算符会返回与bool不同的类型?
---------编辑---------
我的目的不是为了使其无效,而是将v存储216存储在其存储位置.替代程序可能如下所示:
bool v;
int a = 216;
memcpy(&v, &a, sizeof(bool));
std::cout << (v == true) << endl;
Run Code Online (Sandbox Code Playgroud)
或者我可以使用未初始化的bool指针指向一些随机值,恰好是例如216.
#include <iostream>
int main() {
#if __has_include(<coroutine>)
std::cout << "__has_include(<coroutine>)" << std::endl;
#endif
#if defined(__cpp_impl_coroutine)
std::cout << "__cpp_impl_coroutine is defined." << std::endl;
#endif
#if defined(__cpp_coroutines)
std::cout << "__cpp_coroutines is defined." << std::endl;
#else
std::cout << "__cpp_coroutines IS NOT defined!" << std::endl;
#endif
}
Run Code Online (Sandbox Code Playgroud)
我的编译器是 clang-18.1.0。
使用 构建代码clang++ -std=c++20 -stdlib=libc++ ./main.cpp,输出为:
__has_include(<coroutine>)
__cpp_impl_coroutine is defined.
__cpp_coroutines IS NOT defined!
Run Code Online (Sandbox Code Playgroud)
为什么最新的 clang 没有定义功能测试宏 __cpp_coroutines?
compiler-bug ×6
c++ ×3
c++20 ×2
c ×1
c# ×1
clang ×1
cocoa ×1
gcc ×1
objective-c ×1
operators ×1