为什么模板类的静态const成员函数不能出现在常量表达式中

时间:2015-03-31 20:26:57

标签: c++ templates gcc

我有一个类似下面的模板类

typedef void (*single_tick_data_handler) (DCF77::tick_t decoded_data);
typedef void (*flush_handler) ();
template <single_tick_data_handler process_single_tick_data, flush_handler flush>
struct DCF77_Demodulator : public Binning::Convoluter<uint16_t, 100> {
    // details omitted
}

template <typename Frequency_Control> struct DCF77_Clock_Controller {
    static void process_single_tick_data(const DCF77::tick_t tick_data) {
        // some code
    }
    static void flush() {
        // more code
    }

    static DCF77_Demodulator<process_single_tick_data, flush> Demodulator;
}

现在,如果我尝试将模板实例化为

DCF77_Clock_Controller<DCF77_Frequency_Control> foo;

编译器会抱怨。

-I/home/udo/Desktop/arduino-1.6.0/hardware/arduino/sam/variants/arduino_due_x -I/home/udo/Desktop/arduino-1.6.0/libraries/dcf77 /tmp/build3762783334603310985.tmp/Swiss_Army_Debug_Helper.cpp -o /tmp/build3762783334603310985.tmp/Swiss_Army_Debug_Helper.cpp.o 
In file included from Swiss_Army_Debug_Helper.ino:19:0:
/home/udo/Desktop/arduino-1.6.0/libraries/dcf77/dcf77.h: In instantiation of 'struct DCF77_Clock_Controller<DCF77_Frequency_Control>':
Swiss_Army_Debug_Helper.ino:711:29:   required from here
/home/udo/Desktop/arduino-1.6.0/libraries/dcf77/dcf77.h:1897:63: error: 'static void DCF77_Clock_Controller<Frequency_Control>::process_single_tick_data(DCF77::tick_t) [with Frequency_Control = DCF77_Frequency_Control]' cannot appear in a constant-expression
     static DCF77_Demodulator<process_single_tick_data, flush> Demodulator;

当然,如果我想将它用作模板参数,则process_single_tick_data必须是常量表达式。但是,只要我实例化时钟控制器,process_single_tick数据就应该在编译时可以解析。编译器甚至在错误消息中向我解释了如何解析此模板。所以我假设这个静态成员函数是一个传递给DCF77_Demodulator模板的有效参数。不幸的是编译器不同意。但为什么?我错过了什么?

0 个答案:

没有答案