标签: metaprogramming

什么是元编程?

关于这个问题,有人可以解释并发布元编程的示例代码吗?我用Google搜索了这个词,但我没有找到任何例子来说服它可以用于任何实际用途.

同样,Qt的元对象系统是元编程的一种形式吗?

JRH

c++ qt metaprogramming

15
推荐指数
4
解决办法
4085
查看次数

在Javascript中元编程是否可行?

在我的日常工作中,我碰巧编写了链接的javascript函数,类似于LINQ表达式来查询JSON结果.

var Result = from(obj1).as("x").where("x.id=5").groupby("x.status").having(count("x.status") > 5).select("x.status");
Run Code Online (Sandbox Code Playgroud)

它完美地工作并给出预期的结果.

如果代码是这样写的(以更易读的方式),我想知道这看起来很棒

var Result = from obj1 as x where x.status
groupby x.status having count(x.status)  > 5
select x.status;
Run Code Online (Sandbox Code Playgroud)

有没有办法实现这个?

干杯

Ramesh Vel

javascript dsl metaprogramming

15
推荐指数
4
解决办法
6768
查看次数

使用boost :: mpl的类型列表的排列

我正在尝试创建一个包含给定类型列表的排列的列表.

下面的代码似乎起作用,虽然没有预期的结果,当我使用指定的列表而不是通过从实际输入中删除来生成新列表.下面的permutation_helper和broken_helper之间的区别证明了这一点.

有谁知道为什么mpl::remove在这种情况下似乎没有像预期的那样发挥作用?

#include <boost/mpl/list.hpp>
#include <boost/mpl/transform.hpp>
#include <boost/mpl/fold.hpp>
#include <boost/mpl/push_front.hpp>
#include <boost/mpl/joint_view.hpp>
#include <boost/mpl/remove.hpp>
#include <boost/mpl/assert.hpp>
#include <boost/mpl/equal.hpp>

namespace mpl = boost::mpl;

struct test_type1 {};
struct test_type2 {};
struct test_type3 {};

template< typename T >
struct permutations;

template <typename value>
struct permutations<mpl::list1< value > >: mpl::list1<mpl::list1< value > > {};

template< typename value, typename T>
struct permutation_helper: 
  mpl::transform< typename permutations< 
      mpl::list1<test_type3> >::type, 
    mpl::push_front< mpl::_1, value> > { };

template< typename value, typename T>
struct broken_helper:
  mpl::transform< typename …
Run Code Online (Sandbox Code Playgroud)

c++ boost metaprogramming boost-mpl

15
推荐指数
1
解决办法
1520
查看次数

C++ 11 constexpr函数的参数在模板参数中传递

这曾经在几周前工作:

template <typename T, T t>
T            tfunc()
{
    return t + 10;
}

template <typename T>
constexpr T       func(T t)
{
    return tfunc<T, t>();
}

int main()
{
    std::cout << func(10) << std::endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

但现在g++ -std=c++0x说:

main.cpp: In function ‘constexpr T func(T) [with T = int]’:
main.cpp:29:25:   instantiated from here
main.cpp:24:24: error: no matching function for call to ‘tfunc()’
main.cpp:24:24: note: candidate is:
main.cpp:16:14: note: template<class T, T t> T tfunc()
main.cpp:25:1: warning: control reaches …
Run Code Online (Sandbox Code Playgroud)

c++ gcc metaprogramming constexpr c++11

15
推荐指数
1
解决办法
9241
查看次数

Haskell quasiquotation如何用于替换Haskell级别的令牌?

haskellwiki中描述的quasiquotation主要显示为在Haskell中嵌入其他语言的有用工具,而不会弄乱字符串引用.

问题是:对于Haskell本身来说,将现有的Haskell代码放入quasiquoter以便仅仅替换令牌并将结果传递给ghc是多么容易?也许模板Haskell在这里很关键?

我找了代码示例,但没有找到任何代码示例.一些EDSL可以通过减少其组合运算符的大小而受益于此功能(例如,将'a.|.b.>>.c'转换为'[myedsl | a | b >> c]').

dsl haskell metaprogramming

15
推荐指数
1
解决办法
1034
查看次数

如何在编译时计算类成员的偏移量?

给定C++中的类定义

class A
{
  public:
    //methods definition
    ....

  private:
    int i;
    char *str;
    ....
}
Run Code Online (Sandbox Code Playgroud)

是否可以使用C++模板元编程在编译时计算类成员的偏移量?该类不是POD,并且可以具有虚方法,原始数据和对象数据成员.

c++ templates metaprogramming member offset

15
推荐指数
3
解决办法
2万
查看次数

c ++ 11可变参数编程,如何定义矢量塔

如何(如果可能的话)我可以使用c ++ 11可变参数编程来定义vector函数体中的一系列函数(或者换句话说,一个N具有递减N的s 的序列直到0),如下面的变量?

vector<vector<vector<int>>> v<3>;
vector<vector<int>> v<2>;
vector<int> v<1>;
int v<0>;
Run Code Online (Sandbox Code Playgroud)

我想象的是:

#include <iostream>
#include <vector>
using namespace std;

template<int ...> struct seq {};
template<int N, int ...S> struct gens : gens<N-1, N-1, S...> {};
template<int ...S> struct gens<0, S...>{ typedef seq<S...> type; };

template<int ...S>
void f(seq<S...>) {
  //how do I write definitions of v<N> here?
  vector<vector<...(N layers)<vector<int> ...> v<N>;     //??how-to, not valid c++
  vector<vector<...(N -1 layers)<vector<int> ...> v<N-1>;//??how-to, not valid c++ …
Run Code Online (Sandbox Code Playgroud)

c++ templates metaprogramming c++11 c++14

15
推荐指数
2
解决办法
1165
查看次数

为什么在私有部分中声明委托方法是公开的?

我可以通过将声明放在一个部分中来使attr_reader(和相关的attr_writerattr_accessor)方法私有化private:

class Foo
private
  attr_reader :b
end

Foo.new.b # => NoMethodError: private method `b' called for #<Foo:>
Run Code Online (Sandbox Code Playgroud)

但是,Rails' delegate和Ruby标准库def_delegate不能以这种方式工作.这些委托方法始终是公开的.

class Foo
  attr_reader :b
  def initialize
    @b = 'b'
  end
end

require 'forwardable'
class Bar
  attr_reader :foo
  def initialize
    @foo = Foo.new
  end
  extend Forwardable
private
  def_delegator :foo, :b
end

Bar.new.b # => "b"
Run Code Online (Sandbox Code Playgroud)

通过将委托更改为以下内容,可以轻松完成委托授权:

private def_delegator :foo, :b
Run Code Online (Sandbox Code Playgroud)

但我预计会NoMethodError出现Bar.new.b上述错误.为什么代表团不私有?

def_delegator(别名for def_instance_delegator)的方法定义就是rescue(删除了块): …

ruby delegates metaprogramming ruby-on-rails

15
推荐指数
1
解决办法
660
查看次数

使用Roslyn在引用的程序集中获取接口实现

我想在我正在开发的框架中绕过一些经典的汇编扫描技术.

所以,说我已经定义了以下合同:

public interface IModule
{

}
Run Code Online (Sandbox Code Playgroud)

这就是说Contracts.dll.

现在,如果我想发现这个接口的所有实现,我们可能会做类似以下的事情:

public IEnumerable<IModule> DiscoverModules()
{
    var contractType = typeof(IModule);
    var assemblies = AppDomain.Current.GetAssemblies() // Bad but will do
    var types = assemblies
        .SelectMany(a => a.GetExportedTypes)
        .Where(t => contractType.IsAssignableFrom(t))
        .ToList();

    return types.Select(t => Activator.CreateInstance(t));
}
Run Code Online (Sandbox Code Playgroud)

不是一个很好的例子,但它会做.

现在,这些类型的汇编扫描技术可能完全不足,而且它们都在运行时完成,通常会影响启动性能.

在新的DNX环境中,我们可以使用ICompileModule实例作为元编程工具,因此您可以将实现捆绑ICompileModuleCompiler\Preprocess项目中的文件夹中,并让它做一些时髦的事情.

我的目标是使用一个ICompileModule实现来完成我们在运行时在编译时所做的工作.

  • 在我的引用(包括编译和程序集)和我当前的编译中,发现所有可实现的实例 IModule
  • 创建一个类,让我们ModuleList用一个实现来调用它,该实现产生每个模块的实例.
public static class ModuleList
{
    public static IEnumerable<IModule>() GetModules()
    {
        yield return new Module1();
        yield return new Module2();
    } …
Run Code Online (Sandbox Code Playgroud)

c# metaprogramming roslyn dnx

15
推荐指数
2
解决办法
1574
查看次数

带有两个不同类型参数的隐式模板类型推导

假设以下情况:

类型A和类型B,B可以隐式转换为A但反之则是不真实的.

我有一个功能

template<class T>
void do_stuff(T a, T b);
Run Code Online (Sandbox Code Playgroud)

我想这样调用所述函数:

do_stuff(A{}, B{});
Run Code Online (Sandbox Code Playgroud)

这里的问题是编译器无法推断出类型,而是说:

template argument deduction/substitution failed
Run Code Online (Sandbox Code Playgroud)

我可以这样调用我的函数:

do_stuff<A>(A{}, B{});
Run Code Online (Sandbox Code Playgroud)

但这对用户来说更烦人.

或者我可以做这样的事情:

template<class T, class M>
void do_stuff(T a, M b);
Run Code Online (Sandbox Code Playgroud)

但是b继续以它的快乐方式成为B类(具有先前的调用).

理想情况下,我想要像:

template<class T, class M = T>
void do_stuff(T a, M b);
Run Code Online (Sandbox Code Playgroud)

要么:

template<class T@INSERT MAGIC SO THAT T IS DEDUCED AS BEING THE TYPE OF ARGUMENT NR 1@>
void do_stuff(T a, T b);
Run Code Online (Sandbox Code Playgroud)

这样的事情可能吗?

c++ templates metaprogramming template-meta-programming c++17

15
推荐指数
3
解决办法
1123
查看次数