这更像是一个设计问题,而不是一个编码问题。假设有以下架构:
// schema.prisma
// Solution 1
model Entity {
id Int @id @default(autoincrement())
attrs EntityAttr[]
}
model EntityAttr {
id Int @id @default(autoincrement())
value Json // or String, doesnt matter much here
// the point is I need to attach info on the
// join table of this relation
attr Attr @relation(fields: [attrId], references: [id])
entity Entity @relation(fields: [entityId], references: [id])
entityId Int
attrId Int
@@unique([entityId, attrId])
}
model Attr {
id Int @id @default(autoincrement())
entities EntityAttr[]
}
Run Code Online (Sandbox Code Playgroud)
// Solution …Run Code Online (Sandbox Code Playgroud) #include <iostream>
#include <cassert>
struct MyEnum {
enum valid { CASE1, CASE2, DEFAULT };
valid value;
MyEnum(valid value = DEFAULT) : value(value) {}
};
class Base {
protected:
MyEnum value;
public:
Base() = default;
Base(MyEnum value) : value(value) {
std::cout << "Base::Base(MyEnum).\n";
}
};
class ReadableBase : public virtual Base {
public:
ReadableBase() = default;
ReadableBase(MyEnum value) : Base(value) {
std::cout << "ReadableBase::ReadableBase(MyEnum).\n";
}
public:
MyEnum read() { return value; }
};
class WriteableBase : public …Run Code Online (Sandbox Code Playgroud) 考虑以下场景:
template <
typename T,
bool B = std::is_default_constructible_v<T>>
class toy_example;
template<typename T>
class toy_example<T, true>
{
public:
toy_example() = default; // e.g. for default constructible types
toy_example(const T& value);
public:
void monomorphic(T);
private:
T m_value;
};
template<typename T>
class toy_example<T, false>
{
public:
toy_example() = delete; // e.g. for non-default constructible types
toy_example(const T& value); // Repeated declaration
public:
void monomorphic(T); // Repeated declaration
private:
T m_value;
};
// Implementation
template<typename T>
toy_example<T, true>::toy_example(const T& value) : m_value(value) {} …Run Code Online (Sandbox Code Playgroud)