metaprogramming

Using SFINAE to check if the type is complete or not [duplicate]

邮差的信 提交于 2019-12-18 14:33:23
问题 This question already has answers here : How to write `is_complete` template? (7 answers) Closed 5 years ago . Is it possible to check with SFINAE if the type is completely defined? E.g. template <class T> struct hash; template <> struct hash<int> {}; // is_defined_hash_type definition... enum Enum { A, B, C, D }; static_assert ( is_defined_hash_type<int> ::value, "hash<int> should be defined"); static_assert (! is_defined_hash_type<Enum>::value, "hash<Enum> should not be defined"); The

compile-time string hashing

99封情书 提交于 2019-12-18 13:13:01
问题 I need to use a string as the ID to obtain some object. At implement this in a run-time, and works well. But this makes the static type checking impossible, for obvious reasons. I've Googled for the algorithm for calculating the hash-sum of string in the compile-time: C++ compile-time string hashing with Boost.MPL. It seems to be the perfect solution to my problem, except that the sring which is necessary to the algorithm should be split into pieces by 4 characters, or character-by-character,

how to detect if a type is an iterator or const_iterator

隐身守侯 提交于 2019-12-18 12:56:29
问题 I'm wondering, if there is a way to check at compile time whether a type T of some iterator type is a const_iterator, or not. Is there some difference in the types that iterators define (value_type, pointer, ...) between iterators and const iterators? I would like to achieve something like this: typedef std::vector<int> T; is_const_iterator<T::iterator>::value // is false is_const_iterator<T::const_iterator>::value // is true 回答1: C++03 Solution: As none of the answer seems correct, here is

How do I use Ruby metaprogramming to add callbacks to a Rails model?

梦想的初衷 提交于 2019-12-18 12:28:36
问题 I wrote a simple Cacheable module that makes it simple to cache aggregate fields in a parent model. The module requires that the parent object implement the cacheable method and a calc_ method for each field that requires caching at the parent level. module Cacheable def cache!(fields, *objects) objects.each do |object| if object.cacheable? calc(fields, objects) save!(objects) end end end def calc(fields, objects) fields.each { |field| objects.each(&:"calc_#{field}") } end def save!(objects)

How do I get a list of files that have been `required` in Ruby?

╄→尐↘猪︶ㄣ 提交于 2019-12-18 11:42:39
问题 This is purely an experiment, but I'm wondering if it's possible to get a list of the require 'd gems at runtime via some kind of metaprogramming. For example, say I have: require 'rubygems' require 'sinatra' require 'nokogiri' # don't know what to do here How can I print out the following at runtime? this app needs rubygems, sinatra, nokogiri 回答1: You can't do this exactly, because requiring one file may require others, and Ruby can't tell the difference between the file that you required

Using yield inside define_method in Ruby

杀马特。学长 韩版系。学妹 提交于 2019-12-18 11:42:37
问题 Is it possible to make yield keyword work inside a block given to define_method? Simple example: class Test define_method :test do |&b| puts b # => #<Proc:...> yield end end Test.new.test { puts "Hi!" } This code produces following error in both Ruby 1.8.7 and 1.9.0: test.rb:4:in `test': no block given (LocalJumpError) from test.rb:8 The strange thing is the b block variable != nil but block_given? returns false. Is it intentional Ruby behaviour not to recognize blocks by Proc objects? Edit:

How do you list included Modules in a Ruby Class?

▼魔方 西西 提交于 2019-12-18 11:22:33
问题 How would you list out the modules that have been included in a specific class in a class hierarchy in Ruby? Something like this: module SomeModule end class ParentModel < Object include SomeModule end class ChildModel < ParentModel end p ChildModel.included_modules #=> [SomeModule] p ChildModel.included_modules(false) #=> [] Listing the ancestors makes the module appear higher in the tree: p ChildModel.ancestors #=> [ChildModel, ParentModel, SomeModule, Object, Kernel] 回答1: As far as I

How do you list included Modules in a Ruby Class?

本秂侑毒 提交于 2019-12-18 11:22:27
问题 How would you list out the modules that have been included in a specific class in a class hierarchy in Ruby? Something like this: module SomeModule end class ParentModel < Object include SomeModule end class ChildModel < ParentModel end p ChildModel.included_modules #=> [SomeModule] p ChildModel.included_modules(false) #=> [] Listing the ancestors makes the module appear higher in the tree: p ChildModel.ancestors #=> [ChildModel, ParentModel, SomeModule, Object, Kernel] 回答1: As far as I

Python, import string of Python code as module

自古美人都是妖i 提交于 2019-12-18 10:54:21
问题 In python you can do something like this to import a module using a string filename, and assign its namespace a variable on the local namespace. x = __import__(str) I'm wondering if there is a related function that will take take a string of Python code, instead of a path to a file with Python code, and return its namespace as a variable. For example, str = "a = 5"; x = importstr(str) print x.a #output is 5 I realize that I could write the string to a file, then use __import__ on it, but I'd

Ruby String to Class Name

偶尔善良 提交于 2019-12-18 10:48:36
问题 I am trying to create a new class to that will inherit from ActiveRecord::Base the class needs to be dynamically generated from a string "general_systems".camelize.singularize = Class.new < ActiveRecord::Base However I keep getting the error: undefined method `singularize=' for "GeneralSystems":String I've also tried to constantize the string >> foo = "general_systems".camelize.singularize => "GeneralSystem" >> foo.constantize NameError: uninitialized constant GeneralSystem from /var/lib/gems