4

遵循“现代 C++ 设计”中的技术,我正在实现一个具有各种编译时优化的持久性库。如果该变量派生自给定类,我希望能够将函数分派给模板化成员变量:

template<class T, template <class> class Manager = DefaultManager> class Data
{
private:
   T *data_;

public:
   void Dispatch()
   {
      if(SUPERSUBCLASS(Container, T))
      {
         data_->IKnowThisIsHere();
      }
      else
      {
         Manager<T>::SomeGenericFunction(data_);
      }
   }
}

其中 SUPERSUBCLASS 是用于确定对象继承的编译时宏。当然,在 T 确实从 Container 继承(或 T 是内在类型等)的所有情况下,这都会失败,因为编译器正确地抱怨 IKnowThisIsHere() 不是数据成员,即使永远不会遵循此代码路径,使用 T = int 进行预处理后,如下所示:

private:
   int *data_;

public:
   void Dispatch()
   {
      if(false)
      {
         data_->IKnowThisIsHere();

编译器清楚地抱怨这段代码,即使它永远不会被执行。使用 dynamic_cast 的建议也不起作用,因为在编译时再次尝试了不可能的类型转换(例如使用 T=double,std::string):

void Dispatch()
   {
      if(false)
      {
         dynamic_cast<Container*>(data_)->IKnowThisIsHere();

error: cannot dynamic_cast '((const Data<double, DefaultManager>*)this)->Data<double, DefaultManager>::data_' (of type 'double* const') to type 'class Container*' (source is not a pointer to class)
error: cannot dynamic_cast '((const Data<std::string, DefaultManager>*)this)->Da<sttad::string, DefaultManager>::data_' (of type 'struct std::string* const') to type 'class Container*' (source type is not polymorphic)

如果 T 确实从 Container 继承,我真的需要模拟(或确实说服!)让编译器发出一组代码,如果不是,则发出另一组代码。

有什么建议么?

4

6 回答 6

3

正如Alexandrescu在他的《现代 C++ 设计》一书中提出的,重载对于实现编译时调度很有用。

您可以使用这样的类在编译时将布尔值或整数转换为类型:

template <bool n>
struct int2type
{ enum { value = n}; };

以下源代码显示了一个可能的应用程序:

#include <iostream>

#define MACRO()   true  // <- macro used to dispatch 

template <bool n>
struct int2type
{ enum { value = n }; };

void method(int2type<false>)
{ std::cout << __PRETTY_FUNCTION__  << std::endl; }

void method(int2type<true>)
{ std::cout << __PRETTY_FUNCTION__  << std::endl; }

int
main(int argc, char *argv[])
{
    // MACRO() determines which function to call
    //

    method( int2type<MACRO()>()); 

    return 0;
}

当然,真正完成这项工作的是 MACRO() 或作为元函数的更好实现

于 2008-09-28T14:30:06.113 回答
2

您需要一种 compile-time if。然后根据哪种情况调用一个函数true。这样,编译器就不会偶然发现它无法编译的代码(因为它被安全地存储在另一个永远不会被实例化的函数模板中)。

有几种方法可以实现这样的 compile-time if。最常见的是使用 SFINAE 成语:substitution failure is not an error。Boost 的is_base_ofist 实际上就是这个习语的一个实例。要正确使用它,您不会将其写在if表达式中,而是将其用作函数的返回类型。

未经测试的代码:

void Dispatch()
{
    myfunc(data_);
}

private:

// EDIT: disabled the default case where the specialisation matched
template <typename U>
typename enable_if_c<is_base_of<Container, U>::value, U>::type myfunc(U& data_) {
    data_->IKnowThisIsHere();
}

template <typename U>
typename disable_if_c<is_base_of<Container, U>::value, U>::type myfunc(U& data_) { // default case
    Manager<U>::SomeGenericFunction(data_);
}
于 2008-09-28T15:19:37.007 回答
1

Boost 特征有一些东西:is_base_of

于 2008-09-28T13:17:31.903 回答
0

查看 boost 模板元编程库。此外,根据您要完成的任务,查看 boost 序列化库,因为它可能已经包含您需要的内容。

于 2008-09-28T13:18:52.773 回答
0

不幸的是,我也经历过(它也是一个运行时调用;))如果您以与以前类似的方式传入非多态或类类型,编译器会抱怨:

error: cannot dynamic_cast '((const Data<double, DefaultManager>*)this)->Data<double, RawManager>::data_' (of type 'double* const') to type 'class Container*' (source is not a pointer to class)

或者

error: cannot dynamic_cast '((const Data<std::string, DefaultRawManager>*)this)->Data<std::string, DefaultManager>::data_' (of type 'struct std::string* const') to type 'class Container*' (source type is not polymorphic)
于 2008-09-28T13:44:46.237 回答
-1

作为一种教育好奇心,我对“从第一原则”做这件事很感兴趣。但是,我将查看 Boost 库。

无论如何,我认为 is_base_of 没有任何帮助 - 它与 SUPERSUBCLASS 宏完全相同......

于 2008-09-28T13:20:50.133 回答