C++ 成员函数也有一个模板: #include <iostream> #include <type_traits> constexpr bool condition = true; template <class... Opts> class rqueue { using off_t = std::conditional_t<condition, int, void>; public: template <typename T = void> std::enable_if_t<!std::is_same<off_t, void>::value, off_t> start() { return start_(); } private: off_t start_() { std::cout << "start_()" << std::endl; return 1; } };
选项1 利用C++ 成员函数也有一个模板: #include <iostream> #include <type_traits> constexpr bool condition = true; template <class... Opts> class rqueue { using off_t = std::conditional_t<condition, int, void>; public: template <typename T = void> std::enable_if_t<!std::is_same<off_t, void>::value, off_t> start() { return start_(); } private: off_t start_() { std::cout << "start_()" << std::endl; return 1; } };,c++,template-meta-programming,C++,Template Meta Programming,选项1 利用static\u assert以及模板的成员函数仅在上下文需要时根据需要实例化的事实: #include <iostream> #include <type_traits> constexpr bool condition = true; template <class... Opts> class rqueue { using off_t = std::conditional_t<condition, int, void>
static\u assert
以及模板的成员函数仅在上下文需要时根据需要实例化的事实:
#include <iostream>
#include <type_traits>
constexpr bool condition = true;
template <class... Opts>
class rqueue
{
using off_t = std::conditional_t<condition, int, void>;
public:
off_t start()
{
static_assert(!std::is_same<off_t, void>::value, "!");
return start_();
}
private:
off_t start_()
{
std::cout << "start_()" << std::endl;
return 1;
}
};
选择2
通过使成员函数也成为模板,使用一些丑陋的SFINAE:
#include <iostream>
#include <type_traits>
constexpr bool condition = true;
template <class... Opts>
class rqueue
{
using off_t = std::conditional_t<condition, int, void>;
public:
template <typename T = void>
std::enable_if_t<!std::is_same<off_t, void>::value, off_t> start()
{
return start_();
}
private:
off_t start_()
{
std::cout << "start_()" << std::endl;
return 1;
}
};
选项1
利用static\u assert
以及模板的成员函数仅在上下文需要时根据需要实例化的事实:
#include <iostream>
#include <type_traits>
constexpr bool condition = true;
template <class... Opts>
class rqueue
{
using off_t = std::conditional_t<condition, int, void>;
public:
off_t start()
{
static_assert(!std::is_same<off_t, void>::value, "!");
return start_();
}
private:
off_t start_()
{
std::cout << "start_()" << std::endl;
return 1;
}
};
选择2
通过使成员函数也成为模板,使用一些丑陋的SFINAE:
#include <iostream>
#include <type_traits>
constexpr bool condition = true;
template <class... Opts>
class rqueue
{
using off_t = std::conditional_t<condition, int, void>;
public:
template <typename T = void>
std::enable_if_t<!std::is_same<off_t, void>::value, off_t> start()
{
return start_();
}
private:
off_t start_()
{
std::cout << "start_()" << std::endl;
return 1;
}
};
opt::features是否始终使用带有一个模板类型参数的模板?每个功能都是一个结构(可能是带有其他参数的模板,但作为类传递,而不是作为模板传递)。我认为那没那么重要。有关更多详细信息,请参见链接问题
fill_empty
接受两个参数-类型和值:template struct fill_empty:tag::fill_empty{typedef T type;static constexpr T value=V;}代码>好的,也许我不明白你的目的是什么,简言之:你总是实现start()
成员函数,但是当你的类从virtual\u offset
继承时,你希望它调用virtual\u offset::start()
,否则无法访问?为什么静态断言(!std::is\u same::value,!”)
在publicstart()
中,成员函数不是一个选项?opt::features是否总是带有一个模板类型参数的模板?每个功能都是一个结构(可能是带有其他参数的模板,但作为类传递,而不是作为模板传递)。我认为那没那么重要。有关更多详细信息,请参见链接问题fill_empty
接受两个参数-类型和值:template struct fill_empty:tag::fill_empty{typedef T type;static constexpr T value=V;}代码>好的,也许我不明白你的目的是什么,简言之:你总是实现start()
成员函数,但是当你的类从virtual\u offset
继承时,你希望它调用virtual\u offset::start()
,否则无法访问?为什么静态断言(!std::is\u same::value,!”)
在publicstart()
中,成员函数不是一个选项?opt::features是否总是带有一个模板类型参数的模板?每个功能都是一个结构(可能是带有其他参数的模板,但作为类传递,而不是作为模板传递)。我认为那没那么重要。有关更多详细信息,请参见链接问题fill_empty
接受两个参数-类型和值:template struct fill_empty:tag::fill_empty{typedef T type;static constexpr T value=V;}代码>好的,也许我不明白你的目的是什么,简言之:你总是实现start()
成员函数,但是当你的类从virtual\u offset
继承时,你希望它调用virtual\u offset::start()
,否则无法访问?为什么静态断言(!std::is\u same::value,!”)
在publicstart()
中,成员函数不是一个选项?opt::features是否总是带有一个模板类型参数的模板?每个功能都是一个结构(可能是带有其他参数的模板,但作为类传递,而不是作为模板传递)。我认为那没那么重要。有关更多详细信息,请参见链接问题fill_empty
接受两个参数-类型和值:template struct fill_empty:tag::fill_empty{typedef T type;static constexpr T value=V;}代码>好的,也许我不明白你的目的是什么,简言之:你总是实现start()
成员函数,但是当你的类从virtual\u offset
继承时,你希望它调用virtual\u offset::start()
,否则无法访问?为什么静态断言(!std::is\u same::value,!”)公共start()
中的code>成员函数不是一个选项?第三个版本在某种程度上类似于我使用这些扩展程序的工作解决方案,但看起来更好(通过传递条件而不是我的merge
),并且完全满足要求(如果不需要,则不使用方法)。非常感谢。第三个版本在某种程度上类似于我使用这些扩展程序的工作解决方案,但看起来更好(通过传递条件而不是通过我的合并
),并且完全满足要求(如果不需要,通过不使用方法)。非常感谢。第三个版本在某种程度上类似于我使用这些扩展程序的工作解决方案,但看起来更好(通过传递条件而不是通过我的合并
),并且完全满足要求(如果不需要,通过不使用方法)。非常感谢。第三个版本在某种程度上类似于我使用这些扩展程序的工作解决方案,但看起来更好(通过传递条件而不是通过我的合并
),并且完全满足要求(如果不需要,通过不使用方法)。谢谢。
#include <iostream>
#include <type_traits>
constexpr bool condition = true;
template <class... Opts>
class rqueue
{
using off_t = std::conditional_t<condition, int, void>;
public:
template <typename T = void>
std::enable_if_t<!std::is_same<off_t, void>::value, off_t> start()
{
return start_();
}
private:
off_t start_()
{
std::cout << "start_()" << std::endl;
return 1;
}
};
error: no type named 'type' in 'struct std::enable_if<false, void>'
#include <iostream>
#include <type_traits>
constexpr bool condition = true;
template <bool condition, typename CRTP>
struct start_base {};
template <typename CRTP>
struct start_base<true, CRTP>
{
int start()
{
return static_cast<CRTP*>(this)->start_();
}
};
template <class... Opts>
class rqueue : public start_base<condition, rqueue<Opts...>>
{
friend class start_base<condition, rqueue<Opts...>>;
private:
int start_()
{
std::cout << "start_()" << std::endl;
return 1;
}
};
error: 'class rqueue<>' has no member named 'start'