是否可以定义一组模板参数并有条件地专门化它
Is it possible to define a group of template parameters and specialize it conditionally
struct TypeA {
using data_t = int;
enum { thread_create = pthread_create }; // ???
};
struct TypeB {
using data_t = double;
enum { thread_create = another_kind_of_thread_create }; // ???
};
template<typename T>
class Test {
public:
void func() {
T::thread_create(); // ???
}
private:
T::data_t a;
};
Test<TypeA> t1;
Test<TypeB> t2;
我想做的是用一个模板参数专门化模板 class Test
。
如您所见,data_t
应该没有问题,但传递函数似乎并不容易。这段代码会产生一个错误:error: enumerator value for 'thread_create' must have integral or unscoped enumeration type
.
是否可以传递这样的函数?
您描述的是一个非常普通的特征 class。您可能正在寻找以下方面的内容:
struct TypeA {
using data_t = int;
static void thread_create() { pthread_create() };
};
这将允许 T::thread_create();
语法。
如果我没有理解错的话,你想根据专业化来切换线程创建函数和数据类型。
如果是这样,为什么不这样呢?
#include <type_traits>
void pthread_create(void*, void*);
void another_kind_of_thread_create(void*, void*);
//my fakes. use your includes here
struct TypeA {
using data_t = int;
using thread_create_t = void (*)(void*, void*);
static constexpr thread_create_t thread_create{pthread_create};
};
struct TypeB {
using data_t = double;
using thread_create_t = void (*)(void*, void*);
static constexpr thread_create_t thread_create{another_kind_of_thread_create};
};
template<typename T>
class Test : public T {
public:
void func() {
T::thread_create(this, &a); //whatever, put here just to match the prototype...
}
private:
typename T::data_t a;
};
Test<TypeA> t1;
Test<TypeB> t2;
static_assert(std::is_same<decltype(t1)::data_t, int>::value, "t1 should be int");
static_assert(std::is_same<decltype(t2)::data_t, double>::value, "t2 should be double");
struct TypeA {
using data_t = int;
enum { thread_create = pthread_create }; // ???
};
struct TypeB {
using data_t = double;
enum { thread_create = another_kind_of_thread_create }; // ???
};
template<typename T>
class Test {
public:
void func() {
T::thread_create(); // ???
}
private:
T::data_t a;
};
Test<TypeA> t1;
Test<TypeB> t2;
我想做的是用一个模板参数专门化模板 class Test
。
如您所见,data_t
应该没有问题,但传递函数似乎并不容易。这段代码会产生一个错误:error: enumerator value for 'thread_create' must have integral or unscoped enumeration type
.
是否可以传递这样的函数?
您描述的是一个非常普通的特征 class。您可能正在寻找以下方面的内容:
struct TypeA {
using data_t = int;
static void thread_create() { pthread_create() };
};
这将允许 T::thread_create();
语法。
如果我没有理解错的话,你想根据专业化来切换线程创建函数和数据类型。 如果是这样,为什么不这样呢?
#include <type_traits>
void pthread_create(void*, void*);
void another_kind_of_thread_create(void*, void*);
//my fakes. use your includes here
struct TypeA {
using data_t = int;
using thread_create_t = void (*)(void*, void*);
static constexpr thread_create_t thread_create{pthread_create};
};
struct TypeB {
using data_t = double;
using thread_create_t = void (*)(void*, void*);
static constexpr thread_create_t thread_create{another_kind_of_thread_create};
};
template<typename T>
class Test : public T {
public:
void func() {
T::thread_create(this, &a); //whatever, put here just to match the prototype...
}
private:
typename T::data_t a;
};
Test<TypeA> t1;
Test<TypeB> t2;
static_assert(std::is_same<decltype(t1)::data_t, int>::value, "t1 should be int");
static_assert(std::is_same<decltype(t2)::data_t, double>::value, "t2 should be double");