标签: metaprogramming

在没有Boost的情况下实现BOOST_DEDUCED_TYPENAME

有以下代码段:

template<typename ValueType>
ValueType any_cast(any & operand)
{
    typedef BOOST_DEDUCED_TYPENAME remove_reference<ValueType>::type nonref;


    nonref * result = any_cast<nonref>(&operand);
    if(!result)
        boost::throw_exception(bad_any_cast());

    // Attempt to avoid construction of a temporary object in cases when 
    // `ValueType` is not a reference. Example:
    // `static_cast<std::string>(*result);` 
    // which is equal to `std::string(*result);`
    typedef BOOST_DEDUCED_TYPENAME boost::mpl::if_<
        boost::is_reference<ValueType>,
        ValueType,
        BOOST_DEDUCED_TYPENAME boost::add_reference<ValueType>::type
    >::type ref_type;

    return static_cast<ref_type>(*result);
}
Run Code Online (Sandbox Code Playgroud)

是否有可能实现BOOST_DEDUCED_TYPENAMEBoost?我只能使用C++11.

c++ boost metaprogramming template-meta-programming c++11

2
推荐指数
1
解决办法
618
查看次数

C++模板元编程:constexpr函数

我正在观看Bjarne Stroustrup的演讲" The Essential C++ ".

在Q&A会话中,关于如何管理繁重的模板编程代码,他提到:"通过constack perfunction,您可以基本上消除每个通过编写普通代码生成值的模板元编程".

constack perfunction只是通过声音胡乱猜测.

请问这项技术的正确用语是什么?所以我可以做一些跟进阅读.

更新:只需将标题修改为"constexpr功能".

c++ templates metaprogramming

2
推荐指数
1
解决办法
2265
查看次数

如何获得模板以支持T*而不是T&?

显然,给定两个模板,一个从T*中推导出T,另一个从T&推导出T,当与原始指针一起使用时,T&模板被实例化,而不是T*(MSVC13):

// throws if the given object's space isn't all zero
void VerifyZero(const void * ptr, size_t size);

template <typename T> void VerifyZero(const T * ptr)
{
    VerifyZero(ptr, sizeof(T));
}

template <typename T> void VerifyZero(const T & ref)
{
    VerifyZero(&ref, sizeof(T));
}
Run Code Online (Sandbox Code Playgroud)

我真的,真的不希望T&取代以下T*:

PODStruct * p = ... /* malloc or something, right now doesn't matter... */
VerifyZero(p);  // <- why in the 7 hells does this resolve to &p, sizeof(PODStruct*)???
Run Code Online (Sandbox Code Playgroud)

显然,我可以省略模板的参考版本,但是当我真的想要通过引用将POD结构清零时,那就太糟糕了.

c++ templates metaprogramming

2
推荐指数
1
解决办法
86
查看次数

编程语言/平台,具有对AST的运行时访问

我正在寻求为一个简短的演示实现一些概念验证演示,其中正在运行的代码知道当前正在执行的代码块的散列"值".例如:

function BBB(a) {
  a = 2 * a;
  print me.hash;          --> "xxxxxxx" (value of BBB-syntax represenation)
  return a;                              
}

function AAA(a, b, c) {
  d = BBB(a);
  print me.hash;          --> "yyyyyyy" (value of AAA-Syntax representation, possibly dependant on value of BBB, but not necessary)
  return d;
}
Run Code Online (Sandbox Code Playgroud)

我本能地转向LISPish语言,但还没有成功使用Scheme.而且我很长时间没有接触过Common LISP,我怀疑它可能会这样做(提示赞赏).它不一定非常快,或者一个受欢迎的平台,可以是最具学术性和最奇怪的平台.这只是一个演示.

有没有人知道一种语言/平台能够开箱即用或者修补相对较少?我更喜欢某种解析/树状的东西,而不是实际的源代码.

programming-languages functional-programming metaprogramming common-lisp

2
推荐指数
1
解决办法
130
查看次数

constexpr对象有没有办法引用/指向其他非静态constexpr对象?

假设我想在编译时使用某种算法构建图形,然后计算图形中结束的节点数.这似乎是constexpr的理想情况,而不是模板元编程,因为目标是计算产生一个值,而不是真正的类型.我有一些有用的代码,但功能是如此新,我担心编译器是宽松的,我可以解释部分标准说我不能这样做.

#include <iostream>

struct A { int x; constexpr A(int i) noexcept : x{i} {} };
struct B { A& a; constexpr B(A& a) noexcept : a{a} {} };

constexpr int foo() { 
    A a{55};
    B b{a};
    return b.a.x;
}

template<int N>
void output()
{
    std::cout << N << std::endl;
}

int main() {
    // to be absolutely sure compile time eval'd,
    // pass as template arg
    constexpr auto b = foo();
    output<b>();
}
Run Code Online (Sandbox Code Playgroud)

这两个ab实例都是在编译时创建的,它们具有相同的生命周期,所以这应该是"安全的".但这a …

c++ standards metaprogramming constexpr c++14

2
推荐指数
1
解决办法
442
查看次数

c ++模板类根据类型通过ref传递构造函数

假设我有一个看起来像这样的类,如果它T是一个简单的类型,double或者通过引用,如果T更复杂,则应该通过值构造.

到目前为止我的代码看起来像这样:

template<class T>
class Val {
  public:
    Val() = default;
    Val(double v) : _v(v) {}

    template<typename U = T>
    Val(const &T v,
        typename std::enable_if<!std::is_same<U,double>::value, bool>::type = 0)
    : _v(v) {}
  private:
    T _v;
};
Run Code Online (Sandbox Code Playgroud)

哪个有效,但感觉非常粗略,因为在构造函数中引入了一个额外的参数.这个问题有更好的解决方案吗?这似乎更适合过载或模板专业化解决方案?对于所有简单类型(,......)int,这通常可以解决吗?floatdouble

c++ templates metaprogramming parameter-passing sfinae

2
推荐指数
1
解决办法
165
查看次数

与c ++ 11中的boost :: mpl :: if_类似

boost::mpl::if_在C++中是否有替代品?我没有机会使用提升.

c++ boost metaprogramming c++11

2
推荐指数
1
解决办法
612
查看次数

如何动态覆盖方法

我想Hash#[]=通过调用方法动态覆盖方法f.以下代码不起作用,因为方法内不允许类定义:

def f
  class Hash
    def []=(k, v)
      ...
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

解决方法是放入class Hash一个单独的文件

def f
  require 'my_hash.rb'
end
Run Code Online (Sandbox Code Playgroud)

我想知道是否有办法避免添加单独的文件.

ruby methods metaprogramming

2
推荐指数
1
解决办法
1046
查看次数

如果没有调用某个函数,则禁止编译代码

如果没有调用特定的函数,在C++中有没有办法禁止编译代码.

想象一下我有一些课:

class CExample
{
public:
    void Init();
    void DoWork();

};
Run Code Online (Sandbox Code Playgroud)

如果没有为类对象调用Init()函数,是否有办法禁止调用DoWork()

我想禁止写这样的代码:

CExample e;
e.DoWork();
Run Code Online (Sandbox Code Playgroud)

并允许此版本:

CExample e;
e.Init();
e.DoWork();
Run Code Online (Sandbox Code Playgroud)

我可以通过元编程以某种方式达到这种行为吗?

c++ templates metaprogramming c++11

2
推荐指数
1
解决办法
195
查看次数

一些javascript元编程和可链接的setter

我需要'可链接'的制定者,允许你做以下事情:

cool_shoes = new Shoes().color('glitter').style('platform')
console.log(cool_shoes.color()) // => 'glitter'
Run Code Online (Sandbox Code Playgroud)

但是我已经厌倦了一遍又一遍地编写相同的getter和setter代码,以及:

function Shoes() { this._color = null; this._style = null; }
Shoes.prototype.constructor = Shoes;

Shoes.prototype.color = function(arg) {
    if (arguments.length === 0) {
        return this._color;  // _slot accessor
    } else {
        this._color = arg;   // _slot setter
        return this;
    };
};

Shoes.prototype.style = function(arg) {
    if (arguments.length === 0) {
        return this._style;  // _slot accessor
    } else {
        this._style = arg;   // _slot setter
        return this;
    };
};
Run Code Online (Sandbox Code Playgroud)

我的意思是,这是有效的,但是当你应该能够做类似的事情时,这是很多的重复: …

javascript fluent metaprogramming

2
推荐指数
1
解决办法
88
查看次数