且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为什么不能将结构作为值作为模板非类型参数传递?

更新时间:2022-01-13 00:17:34

我认为,如果只有三个变量,因为它是一个痛苦的mikta指定正确和实现是主要的原因,我们没有这个。当然,很容易使这个位工作,但人们会抱怨如何使用struct模板参数不工作在所有相同的情况下,其他模板参数做(考虑部分专门化,或者做什么 operator ==

$ c>)。

I think "because it's a pain in the mikta to both specify properly and implement" is the major reason we don't have this. Sure, it would be easy to make just this bit work, but then people would complain about how using struct template parameters does not work in all the same situations the other template parameters do (consider partial specializations, or what to do with operator==).

在我看来,太麻烦得到整个蛋糕, t满意,可能更令人沮丧。只是这个小小的工作不会给我更多的权力,比如下面的,这有额外的优势,使用各种东西(包括部分专业化)开箱。

In my opinion it's too messy to get the whole cake, and getting just one tiny slice isn't satisfying enough, and possibly more frustrating. Making just this tiny bit work won't give me more power than something like the following, which has the additional advantage of working with all kinds of stuff (including partial specializations) out of the box.

template <int X, int Y, int Z>
struct meta_triple {
    // static value getters
    static constexpr auto x = X;
    static constexpr auto y = Y;
    static constexpr auto z = Z;
    // implicit conversion to Triple 
    constexpr operator Triple() const { return { X, Y, Z }; }
    // function call operator so one can force the conversion to Triple with
    // meta_triple<1,2,3>()()
    constexpr Triple operator()() const { return *this; }
};