部分专门化没有参数的函数模板

时间:2016-03-24 20:43:11

标签: c++ templates c++14 template-specialization

我认为这是不可能的,但有人必须知道更好......

template<typename T>   
T Read()   //T is int, char, etc
{
    return read<T>();
}

template<typename T, Size> 
std::array<T,Size> Read<std::array<T, Size>>()
{
     return unique_read<T, Size>();
}

我想,只要我指定任何模板参数,它就不再是完全特化,并且函数中不允许部分特化

我唯一能想到的是:

template<typename T>
struct _dummy
{
    T Read() {
        return T();
    };
};

template<typename T, size_t Size>
struct _dummy<std::array<T, Size>>
{
    using ArrayType = std::array<T, Size>;

    ArrayType Read() {
        return ArrayType();
    };
};

2 个答案:

答案 0 :(得分:3)

你应该使用标签调度来完成这类工作:

library(shiny)

ui <- fluidPage(

  fluidRow(
    radioButtons('original','Normal Radio Button',c('1','2','3','4','5')),
    DT::dataTableOutput("table")
  )
)

答案 1 :(得分:0)

另一种方法是推迟使用函数对象:

template<class T> 
struct reader_op {
  T operator()() const { 
    // whatever needs to happen here
  }
};

// partially specialise for array case
template<class T, size_t N> 
struct reader_op<std::array<T, N>> {
  std::array<T, N> operator()() const { 
    // whatever needs to happen here
  }
};

// reader_op class type is now deduced from T
// when T is a std::array, our specialised function object will
// be used
template<typename T>   
T Read()   //T is int, char, etc
{
    return reader_op<T>()();
}
相关问题