我有一个子模型,它应该能够通过 ActiveRecord::Store 功能存储不同的属性。这些属性应由父模型确定。为此,父模型有一列将content_attributes子模型的属性存储为字符串数组(即['color', 'size', 'age'])。
为了在子实例中拥有父级定义的所有属性的访问器,我当前使用一种解决方法来映射所有可用父级的所有属性名称:
class child
belongs_to :parent
store :content, accessors: Parent.all_content_attributes, coder: JSON
...
end
Run Code Online (Sandbox Code Playgroud)
实际上,我只想为不同父级的所有属性设置访问器。然而,在上面的示例中,子实例将获得一长串可有可无的属性名称。如何更换Parent.all_content_attributes?我猜我需要某种元编程?!
在 C++17 中我知道我可以写:
#include <type_traits>
struct A
{
size_t operator()(double x) const { return 1; };
};
int main()
{
static_assert(std::is_invocable_r_v<size_t, A, double>);
}
Run Code Online (Sandbox Code Playgroud)
但是现在我想使用std::is_invocable来测试任意方法(此处为方法)的存在size(double):
#include <type_traits>
struct A
{
size_t size(double x) const { return 1; };
};
int main()
{
static_assert(std::is_invocable_r_v<size_t, ???, double>);
}
Run Code Online (Sandbox Code Playgroud)
问题是“???”该如何填写?让它发挥作用?
我在 ruby on Rails 中有一个模型,代码如下,它使用 singleton 类定义。另外,还有一些元编程逻辑。但是,我不明白这段代码何时会调用。是在编辑下面指定的属性时吗?
class Product < ApplicationRecord
class << self
['cat_no', 'effort', 'impact', 'effect', 'feedback'].each do |attr|
define_method "update_#{attr}" do |pr, count, user_id|
pr.order=pr.cat_no
pr.idea=pr.description
pr.update("#{attr}"=>count,:last_modified_by=>user_id)
end
end
end
end
Run Code Online (Sandbox Code Playgroud)
请帮忙。谢谢
在 Rust 中是否可以创建新的关键字作为另一个关键字的别名?
例如,创建fun指向fn并使用它的关键字,就好像它是fn:
// somehow alias `fun` to `fn`
fun main() {
println!("Hello, world!")
}
Run Code Online (Sandbox Code Playgroud)
如果可以的话,这将如何实现?
我正在尝试编写一个模板方法来处理 STL 容器中的项目。获取容器的详细信息很容易(并且我使用 std::enable_if 子句来允许仅当容器可以迭代时才调用此模板方法(检测到 begin() 方法。))我还需要了解数据容器所持有的类型。这是有效的:
template <typename CONTAINER>
std::string doStuff(const CONTAINER & container) {
using CONTAINER_TYPE = typename CONTAINER::value_type;
}
Run Code Online (Sandbox Code Playgroud)
if constexpr如果我还可以确定容器中保存的东西的类型,我可以使用此方法来执行某些操作。这是不起作用的代码,但就像我正在尝试的那样:
template <typename CONTAINER, typename ITEM>
std::string doStuff(const CONTAINER<ITEM> & container) {
using CONTAINER_TYPE = typename CONTAINER::value_type;
using ITEM_TYPE = typename ITEM::value_type;
}
Run Code Online (Sandbox Code Playgroud)
为什么我不能以这种方式调用该方法是完全有道理的,但是我可以做什么(调用该方法或在方法内部进行元编程)来确定容器中项目的类型。我想这样做,以便在编译时就知道它。
(我已经尝试了 decltype 和 invoke_result 的几种排列以及大量的搜索,但还没有什么效果。)我尝试过例如:
using ITEM_TYPE = std::invoke_result<&CONTAINER::begin>::type;
Run Code Online (Sandbox Code Playgroud)
当然,这会返回一个需要取消引用的迭代器类型,但“*”在这里似乎没有按预期工作。
想象一下我有两个功能:
void string(const char *str)
{
std::cout << "this is string" << std::endl;
}
void number(const char *str, double f)
{
std::cout << "this is number" << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
我想编写一个通用包装器,以便能够format()像这样调用:
int main() {
format("single arg");
format("format string", 1.0);
format("single arg", "format string", 1.0);
format("format string 1", 1.0, "just string arg", "format string 2", 2.0);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
也就是说,如果参数以 {string, number} 对的形式出现,则调用number(); 否则请致电string(). 显然,只能从右到左解包参数。我尝试按照以下(错误)方式实现它:
template<class T>
void operation(T first)
{
string(first);
}
template<class T, class …Run Code Online (Sandbox Code Playgroud) 在 C++ 中是否可以自动替换模板返回值中的数据类型,而无需在括号中指定特定类型?
我正在练习元编程并尝试执行以下代码。我收到了下面指定的编译错误。
template <typename T>
T func(int value) {
if constexpr (std::is_floating_point_v<T>)
return (float)value;
if constexpr (std::is_same_v<T,std::string>)
return std::to_string(value);
if constexpr (std::is_integral_v<T>)
return value;
}
int main() {
auto o1 = func<int>(6); //Ok int 6
std::string o2 = func(6); // CE
}
Run Code Online (Sandbox Code Playgroud) 我在尝试理解该fmt库时遇到了一些困难。
我们从print函数开始:
fmt::print("Hello, {}", a);
Run Code Online (Sandbox Code Playgroud)
这是 的定义print:
template <typename... T>
FMT_INLINE void print(format_string<T...> fmt, T&&... args) {
// Implementation
}
Run Code Online (Sandbox Code Playgroud)
该print函数采用 aformat_string<T...>作为参数。T...所以我们可以在编译时做一些事情。
但是如何T...在编译时推断呢?
下面是如何format_string实现的(代码取自10.0.0版本)。对于编译时格式字符串,它有一个隐式 constexpr 构造函数,该构造函数采用string_view- 兼容类型作为参数。
template <typename... Args>
using format_string = basic_format_string<char, type_identity_t<Args>...>;
template <typename Char, typename... Args> class basic_format_string {
private:
basic_string_view<Char> str_;
public:
template <typename S,
FMT_ENABLE_IF(
std::is_convertible<const S&, basic_string_view<Char>>::value)>
FMT_CONSTEVAL FMT_INLINE basic_format_string(const S& s) : str_(s) { …Run Code Online (Sandbox Code Playgroud) 我在这里有一段代码,我真的可以在重构时使用一些帮助.我需要在rails中以表格形式添加关系数据的不同方法.代码来自http://railscasts.com/episodes/75-complex-forms-part-3,我的问题是我需要有Material模型和Answer模型的方法.所以我需要完全相同的代码两次,"材料"替换为"答案".
这似乎应该通过一些动态编程来解决?但我对此毫无经验.
这是怎么解决的?
after_update :save_materials
after_update :save_answers
def new_material_attributes=(material_attributes)
material_attributes.each do |attributes|
materials.build(attributes)
end
end
def existing_material_attributes=(material_attributes)
materials.reject(&:new_record?).each do |material|
attributes = material_attributes[material.id.to_s]
if attributes
material.attributes = attributes
else
materials.delete(material)
end
end
end
def save_materials
materials.each do |material|
material.save(false)
end
end
Run Code Online (Sandbox Code Playgroud) 我有一个作者类:
class Author < ActiveRecord::Base
def to_s
name
end
end
Run Code Online (Sandbox Code Playgroud)
定义to_s允许我这样做puts Author.first,但不是puts Author.first.rjust(10):
NoMethodError: undefined method `rjust' for #<Author:0x21eb5d4>
Run Code Online (Sandbox Code Playgroud)
如果Ruby to_s在这种情况下自动尝试字符串方法,那不是更好吗?有没有办法得到这种行为?