Closed. This question needs details or clarity。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗?添加详细信息并通过editing this post阐明问题。
                        
                        2年前关闭。
                                                                                            
                
        
我正在尝试编写一个简单的编译时维度分析库。我想创建一个编译选项来删除库所做的所有操作,而无需更改代码。因此,从本质上讲,我选择了自己的原始类型版本,如果选择了该选项,则想用实际的原始类型替换它们。

这是代码的最小工作示例

#include <iostream>
#include <stdint.h>

#define DEBUG
#ifdef DEBUG
    template<int lenght, int time, int mass, int charge, int temperature, int amount, int intensity>
    struct Dimensions {
        static const int64_t LENGHT = lenght;
        static const int64_t TIME = time;
        static const int64_t MASS = mass;
        static const int64_t CHARGE = charge;
        static const int64_t TEMPERATURE = temperature;
        static const int64_t AMOUNT = amount;
        static const int64_t INTENSITY = intensity;
    };

    typedef Dimensions<  0, 0, 0, 0, 0, 0, 0 > Adimensional;
    typedef Dimensions<  1, 0, 0, 0, 0, 0, 0 > Length;
    typedef Dimensions<  0, 1, 0, 0, 0, 0, 0 > Time;

    template<typename Dims> class Int32 {
    private:
        int32_t m_value;

    public:
        inline Int32() : m_value(0) {}

        inline Int32(int32_t value) : m_value(value) {}

        inline int32_t value() {
            return m_value;
        }
    };

    template<typename Dims>
    Int32<Dims> inline operator+(Int32<Dims> &lhs, Int32<Dims> &rhs) {
        return Int32<Dims>(lhs.value() + rhs.value());
    }

    struct Unmatched_dimensions_between_operands;

    template<typename DimsLhs, typename DimsRhs>
    Unmatched_dimensions_between_operands inline operator+(Int32<DimsLhs> &lhs, Int32<DimsRhs> &rhs);
#else
    template<typename Dims> using Int32<Dims> = std::int32_t;
#endif

int main(int argc, char* argv[]) {
    Int32<Time> a = 2;
    Int32<Time> b = 5;

    std::cout << (a + b).value() << "\n";
    return 0;
}


删除#define DEBUG行时,出现编译错误

Error   C2988   unrecognizable template declaration/definition 59


是否有适当的方法用原始类型替换代码中的任何Int32模板版本?

最佳答案

尝试:

template<typename Dims> using Int32 = std::int32_t;


另外,您还需要以某种方式定义Time(可能还有AdimensionalLength)(无关紧要,因为从不使用template参数)。

编辑:您的程序仍然无法运行,因为您访问的是value的成员Int32,而该成员当然不在std::int32_t中。但是,我希望这能使您走上正确的道路。

10-07 15:56