我在尝试翻转号码的所有位时遇到了一个奇怪的问题。
#include <cstdint>
constexpr uint16_t DefaultValueForPortStatus { 0xFFFF };
void f(uint16_t x)
{
}
int main()
{
f(~(DefaultValueForPortStatus));
}
Run Code Online (Sandbox Code Playgroud)
当我编译这个程序(GCC trunk)时,我收到一个错误:
警告:从“int”到“uint16_t”的无符号转换{aka“short unsigned int”}将值从“-65536”更改为“0”[-Woverflow]
当我从类型说明符中删除 constexpr 时,不会出现警告。这是为什么?为什么编译器将 uint16_t constexpr 变量更改为 int,而在非 constexpr 的情况下一切都很好?
我正在尝试设置我的测试环境,但我在使用 Sqlite 适配器时遇到了问题。每个创建的上下文都有相同的连接,因此应该为每个上下文正确构建内存数据库。
但是当我尝试添加新内容时,它会抛出错误:“没有这样的表:%here_is_my_tablename%”。
我觉得我的配置应该不错。
根据:
public abstract class BaseServiceTests : IDisposable
{
protected readonly SqliteConnection Connection;
public BaseServiceTests()
{
Connection = new SqliteConnection("DataSource=:memory:");
Connection.Open();
Assert.NotNull(Connection);
using (var ctx = BuildCoreContext())
{
ctx.Database.EnsureCreated();
}
using (var ctx = BuildWebContext())
{
ctx.Database.EnsureCreated();
}
}
public void Dispose()
{
Connection.Close();
}
public DbContextOptions<TContext> Options<TContext>() where TContext: DbContext
{
var options = new DbContextOptionsBuilder<TContext>()
.UseSqlite(Connection)
.Options;
return options;
}
public ServiceRequestCoreContext BuildCoreContext()
{
var ctx = new ServiceRequestCoreContext(Options<ServiceRequestCoreContext>(), null);
ctx.Database.OpenConnection();
return ctx;
} …Run Code Online (Sandbox Code Playgroud) 我遇到了一个非常奇怪的 clang 问题。
当我使用 gcc-11 使用 C++20 编译代码时,一切都很好。
当我尝试使用 C++20 和 clang-14 编译它时出现问题(使用 clang-15/16/17 没有帮助)。
链接器抱怨对容器的所有类模板实例化的未定义引用,下面是错误消息之一:
/opt/compiler-explorer/gcc-11.2.0/lib/gcc/x86_64-linux-gnu/11.2.0/../../../../x86_64-linux-gnu/bin/ld: /tmp/example-ded8d6.o:(.rodata._ZTV6WidgetINSt7__cxx114listIlSaIlEEEE[_ZTV6WidgetINSt7__cxx114listIlSaIlEEEE]+0x10): undefined reference to `Widget<std::__cxx11::list<long, std::allocator<long> > >::doSth()'
/opt/compiler-explorer/gcc-11.2.0/lib/gcc/x86_64-linux-gnu/11.2.0/../../../../x86_64-linux-gnu/bin/ld: /tmp/example-ded8d6.o:(.rodata._ZTV6WidgetISt6vectorIbSaIbEEE[_ZTV6WidgetISt6vectorIbSaIbEEE]+0x10): undefined reference to `Widget<std::vector<bool, std::allocator<bool> > >::doSth()'
Run Code Online (Sandbox Code Playgroud)
到目前为止,我已经设法通过更改 Widget 的 doSth 签名来解决这个问题
void doSth() override
Run Code Online (Sandbox Code Playgroud)
到:
__attribute__((used)) void doSth() override
Run Code Online (Sandbox Code Playgroud)
但这是——正如我上面所说的——而是一种解决方法,而不是真正的解决方案。有谁知道为什么 Clang 无法处理这个问题?
下面是编译器资源管理器和 C++ 见解的链接以及最小的可重现示例,这些示例是从我的代码库中提取的,并减少到绝对最小值,以便重现该示例并显示我的意图。
编译器浏览器:https://godbolt.org/z/83dMMvozn
C++ 见解:https://cppinsights.io/s/e35151b2
更新
我删除了不需要的代码部分,并添加了另一个(可接受的)解决方法 - 我没有使用 lambda 作为“调用”调用的参数,而是使用了可调用结构 - 它“神奇地”起作用了 - 但是,我不明白为什么它不适用于 lambda。
#include <memory>
#include <type_traits>
#include <variant>
#include <vector>
#include <list>
struct …Run Code Online (Sandbox Code Playgroud) 我尝试测试我的应用程序,所以我需要模拟我的EF上下文.
我的代码似乎没问题,但我有以下异常:
"System.ArgumentNullException:Value不能为null.参数名称:source"
这是我的测试方法:
var options = new DbContextOptionsBuilder<ProductContext>().Options;
var settings = new SqlSettings
{
InMemory = true
};
var context = new Mock<ProductContext>(options, settings);
var mockTreeService = new TreeService(context.Object);
await mockTreeService.CreateTreeAsync("Testing tree", Guid.NewGuid());
context.Verify(x => x.AddAsync(It.IsAny<Tree>(), CancellationToken.None), Times.Once);
Run Code Online (Sandbox Code Playgroud)
看起来在执行这段代码时抛出了这个expceiton
var tree = await _context.Trees
.Include(x => x.Translation)
.FirstOrDefaultAsync(x => x.Translation.Pl == name);
Run Code Online (Sandbox Code Playgroud)
它来自我正在测试的服务
我有两种配置: - 开发 - 生产
具有以下 json 设置文件: - appsettings.Production.json - appsettings.Development.json
在每个设置文件中,我都有以下部分:
(生产)
"Sql" : {
"Name": "App_Prod",
"Server": "127.0.0.1",
"Port": 0,
"Database": "db_prod",
"Username": "user_prod",
"Password": "secret"
},
Run Code Online (Sandbox Code Playgroud)
(发展)
"Sql" : {
"Name": "App_Dev",
"Server": "127.0.0.1",
"Port": 0,
"Database": "db_dev",
"Username": "user_dev",
"Password": "secret"
},
Run Code Online (Sandbox Code Playgroud)
我将它们注入到我的 DbContext 构造函数中,并且它工作正常。我使用以下命令使用迁移系统更新了我的数据库:
dotnet ef数据库更新--context MyDbContext--配置开发
它有效,并且我的数据库方案已更新。
但是当我尝试使用以下命令更新生产数据库时:
dotnet ef 数据库更新 --context MyDbContext--configuration Production
它向我提供了无需应用任何迁移的信息。
我将连接字符串转储到 MyDbContext 中,我可以看到 DbContext 仍然使用开发配置,即使我输入它应该使用生产设置文件。
为什么迁移机制不尊重我的配置?如何使用 appsettings.Production 运行 dotnet ef?
我有以下代码:
#include <cstddef>
#include <cstdint>
using ReadIteratorType = Buffer::iterator;
using WriteIteratorType = Buffer::iterator;
template <typename TReadIterator, typename TWriteIterator>
class FieldBase
{
public:
using ReadIterator = TReadIterator;
using WriteIterator = TWriteIterator;
virtual void read(ReadIterator& readIterator, std::size_t& len) = 0;
virtual void write(WriteIterator& writeIterator, std::size_t& len) = 0;
};
template <typename TValue>
class Field : public FieldBase<ReadIteratorType, WriteIteratorType>
{
public:
using ValueType = TValue;
virtual const ValueType& getValue() const { return m_value; };
virtual void setValue(const ValueType& value) { m_value = value;} …Run Code Online (Sandbox Code Playgroud) 我有以下代码:
#include <string>
#include <vector>
class A {
public:
std::string s = "test";
};
class B {
public:
std::vector<A> vec;
};
int main()
{
std::vector<B> vec;
A a1 = A();
A a2 = A();
B b1 = B();
b1.vec.push_back(a1);
b1.vec.push_back(a2);
vec.push_back(b1); // push_1
vec.push_back(b1);
}
Run Code Online (Sandbox Code Playgroud)
每当我在调试器下执行这个应用程序,并且执行过程到达带有注释 push_1 的指令时,执行过程就会停止,我的调试器输出中有以下两个信息:
__lhs { s = "测试" }
__rhs { s = "错误读取变量:无法创建地址为 0x0 且长度非零的惰性字符串。}
应用程序退出代码为 0。
但是当我删除 A 类的属性,或者用 int 属性替换时,这种奇怪的行为不会发生。为什么会发生?为什么 A 类中的字符串出现会导致此错误?
我有以下代码:
struct B
{
B(int) {}
};
template <typename T>
class Base : public B
{
friend T;
Base() : B(1) {}
};
class Derived : public Base<Derived>
{
public:
void do_sth() const {}
};
int main()
{
auto x = Derived{}; //Compiles only when using C++11
auto x1 = Derived(); //Compiles using C++17/20 flag
x.do_sth();
x1.do_sth();
}
Run Code Online (Sandbox Code Playgroud)
由于某种原因,当使用 C++17 时,由于“x”变量的“不可编译”初始化,编译失败。编译器说:
Base::Base() [with T = Derived]' 在此上下文中是私有的
但正如您所看到的,下面我创建了一个相同类型的对象,但这次我没有使用统一初始化。x1 变量可以使用 C++11 或 C++17 标准进行编译,但 'x' 变量只能在 C++11 模式下编译。这是为什么?标准发生了什么变化导致了这个问题?
是否可以使用 EF Core 和 fluent api 设置父/子关系?
我有以下课程
public class Category
{
public int Id { get; set; }
public int ParentId { get; set; }
public string Name { get; set; }
public ICollection<Category> SubCategories { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
因此,如果我创建具有如下结构的对象列表,则 EF 可能会使用适当的数字设置实体的 id 和 parentId?
我有以下课程
public class TreeItem
{
public long Id { get; set; }
public Tree Tree { get; set; }
public string Model { get; set; }
public string Version { get; protected set; }
public string Index { get; protected set; }
}
Run Code Online (Sandbox Code Playgroud)
并且我想更改属性树的默认数据库的列名称。作为默认实体框架,在databse中寻找“ TreeId”列,但是我们的名字约定说此列应命名为“ tree_id”
如果我尝试将其设置如下:
modelBuilder.Entity<TreeItem>().Property(t => t.Tree).HasColumnName("tree_id");
Run Code Online (Sandbox Code Playgroud)
Ef向我抛出一条异常消息:
由于实体类型“ TreeItem”已配置为导航属性,因此无法为属性“ Tree”调用“属性”。属性只能用于配置标量属性。
是否可以将属性Tree的默认列名称从“ TreeId”更改为“ tree_id”?