【问题标题】:Problems with Delegate Template Metaprogramming委托模板元编程的问题
【发布时间】:2013-05-13 05:46:23
【问题描述】:

以下代码由Yingle Jia's code 的另一位程序员改编,我不得不将其移植到 Linux。它在 VS2010 中编译得非常好,但是当我尝试使用 gcc 4.6.3 在 Ubuntu 中构建时,它在

处显示错误
template <class R ACF_DELEGATE_COMMA ACF_DELEGATE_TEMPLATE_PARAMS>
class  Delegate<R (ACF_DELEGATE_TEMPLATE_ARGS)>

错误信息是:

../../../mySDK-master/include/abc/DelegateTemplate.h:45:1: error: pasting "," and "class" does not give a valid preprocessing token
../../../mySDK-master/include/abc/DelegateTemplate.h:46:1: error: pasting "," and "T" does not give a valid preprocessing token
../../../mySDK-master/include/abc/DelegateTemplate.h:74:1: error: pasting "," and "a" does not give a valid preprocessing token

第 45 行和第 46 行是我在上面粘贴的 DelegateTemplate.h 的两行代码。

Delegate.h

// Copyright (C) 2004-2005 Yingle Jia
//
// Permission to copy, use, modify, sell and distribute this software is 
// granted provided this copyright notice appears in all copies. 
// This software is provided "as is" without express or implied warranty, 
// and with no claim as to its suitability for any purpose.
//
// AcfDelegate.h
//

#ifndef __Acf_Delegate__
#define __Acf_Delegate__

#ifndef __Acf_Corlib__
#include <stdexcept> // for std::runtime_error
#endif // #ifndef __Acf_Corlib__
#include <utility> // for std::pair

// Macros for template metaprogramming

#define ACF_JOIN(a, b)        ACF_DO_JOIN(a, b)
#define ACF_DO_JOIN(a, b)     ACF_DO_JOIN2(a, b)
#define ACF_DO_JOIN2(a, b)    a##b

#define ACF_MAKE_PARAMS1_0(t)
#define ACF_MAKE_PARAMS1_1(t)    t##1
#define ACF_MAKE_PARAMS1_2(t)    t##1, ##t##2
#define ACF_MAKE_PARAMS1_3(t)    t##1, ##t##2, ##t##3
#define ACF_MAKE_PARAMS1_4(t)    t##1, ##t##2, ##t##3, ##t##4
#define ACF_MAKE_PARAMS1_5(t)    t##1, ##t##2, ##t##3, ##t##4, ##t##5
#define ACF_MAKE_PARAMS1_6(t)    t##1, ##t##2, ##t##3, ##t##4, ##t##5, ##t##6

#define ACF_MAKE_PARAMS2_0(t1, t2)
#define ACF_MAKE_PARAMS2_1(t1, t2)   t1##1 t2##1
#define ACF_MAKE_PARAMS2_2(t1, t2)   t1##1 t2##1, t1##2 t2##2
#define ACF_MAKE_PARAMS2_3(t1, t2)   t1##1 t2##1, t1##2 t2##2, t1##3 t2##3
#define ACF_MAKE_PARAMS2_4(t1, t2)   t1##1 t2##1, t1##2 t2##2, t1##3 t2##3, t1##4 t2##4
#define ACF_MAKE_PARAMS2_5(t1, t2)   t1##1 t2##1, t1##2 t2##2, t1##3 t2##3, t1##4 t2##4, t1##5 t2##5
#define ACF_MAKE_PARAMS2_6(t1, t2)   t1##1 t2##1, t1##2 t2##2, t1##3 t2##3, t1##4 t2##4, t1##5 t2##5, t1##6 t2##6

#define ACF_MAKE_PARAMS1(n, t)         ACF_JOIN(ACF_MAKE_PARAMS1_, n) (t)
#define ACF_MAKE_PARAMS2(n, t1, t2)    ACF_JOIN(ACF_MAKE_PARAMS2_, n) (t1, t2)

namespace CORE
{

#ifndef __Acf_Corlib__
class InvalidOperationException : public std::runtime_error
{
public:
    InvalidOperationException() : std::runtime_error("Invalidate operation")
    {
    }
};
#endif // #ifndef __Acf_Corlib__

template <class TSignature>
class  Delegate; // no body

} 

// Specializations

#define ACF_DELEGATE_NUM_ARGS   0 // Delegate<R ()>
#include "DelegateTemplate.h"
#undef ACF_DELEGATE_NUM_ARGS

#define ACF_DELEGATE_NUM_ARGS   1 // Delegate<R (T1)>
#include "DelegateTemplate.h"
#undef ACF_DELEGATE_NUM_ARGS

#define ACF_DELEGATE_NUM_ARGS   2 // Delegate<R (T1, T2)> 
#include "DelegateTemplate.h"
#undef ACF_DELEGATE_NUM_ARGS

#define ACF_DELEGATE_NUM_ARGS   3 // Delegate<R (T1, T2, T3)>
#include "DelegateTemplate.h"
#undef ACF_DELEGATE_NUM_ARGS

#define ACF_DELEGATE_NUM_ARGS   4 // Delegate<R (T1, T2, T3, T4)>
#include "DelegateTemplate.h"
#undef ACF_DELEGATE_NUM_ARGS

#define ACF_DELEGATE_NUM_ARGS   5 // Delegate<R (T1, T2, T3, T4, T5)>
#include "DelegateTemplate.h"
#undef ACF_DELEGATE_NUM_ARGS

#define ACF_DELEGATE_NUM_ARGS   6 // Delegate<R (T1, T2, T3, T4, T5, T6)>
#include "DelegateTemplate.h"
#undef ACF_DELEGATE_NUM_ARGS

#define N_INVOKE(DELEGATE,PARAMS) { if(DELEGATE) DELEGATE.Invoke PARAMS; }
#define N_EVENT_HANDLER(a) std::make_pair(this,&a);

#endif // #ifndef __Acf_Delegate__

和DelegateTemplate.h的一小部分

// Copyright (C) 2004-2005 Yingle Jia
//
// Permission to copy, use, modify, sell and distribute this software is 
// granted provided this copyright notice appears in all copies. 
// This software is provided "as is" without express or implied warranty, 
// and with no claim as to its suitability for any purpose.
//
// AcfDelegateTemplate.h
//
// Note: this header is a header template and must NOT have multiple-inclusion
// protection.

#define ACF_DELEGATE_TEMPLATE_PARAMS    ACF_MAKE_PARAMS1(ACF_DELEGATE_NUM_ARGS, class T)
    // class T0, class T1, class T2, ...
#define ACF_DELEGATE_TEMPLATE_ARGS      ACF_MAKE_PARAMS1(ACF_DELEGATE_NUM_ARGS, T)
    // T0, T1, T2, ...
#define ACF_DELEGATE_FUNCTION_PARAMS    ACF_MAKE_PARAMS2(ACF_DELEGATE_NUM_ARGS, T, a)
    // T0 a0, T1 a1, T2 a2, ...
#define ACF_DELEGATE_FUNCTION_ARGS      ACF_MAKE_PARAMS1(ACF_DELEGATE_NUM_ARGS, a)
    // a0, a1, a2, ...

//// Comma if nonzero number of arguments 
#if ACF_DELEGATE_NUM_ARGS == 0
    #define ACF_DELEGATE_COMMA  
#else
    #define ACF_DELEGATE_COMMA  ,
#endif

namespace CORE
{

//-------------------------------------------------------------------------
// class Delegate<R (T1, T2, ..., TN)>

template <class R ACF_DELEGATE_COMMA ACF_DELEGATE_TEMPLATE_PARAMS>
class  Delegate<R (ACF_DELEGATE_TEMPLATE_ARGS)>
{
// Declarations
private:
    class DelegateImplBase
    {
    // Fields
    public:
        DelegateImplBase* Previous; // singly-linked list

    // Constructor/Destructor
    protected:
        DelegateImplBase() : Previous(NULL) { }
        DelegateImplBase(const DelegateImplBase& other) : Previous(NULL) { }
    public:
        virtual ~DelegateImplBase() { }

    // Methods
    public:
        virtual DelegateImplBase* Clone() const = 0;
        virtual R Invoke(ACF_DELEGATE_FUNCTION_PARAMS) const = 0;
    };

是因为 gcc 中缺少模板元编程支持吗?我检查了它是否是因为可变参数模板,但这应该在 gcc 4.3 本身中得到修复。如果您能帮助解决此错误,我们将不胜感激。

【问题讨论】:

  • 这里没有可变参数模板,只是预处理器黑客尝试解决 C++ 没有可变参数模板的事实。这种代码正是引入它们的原因。
  • 删除The token pasting operator ## 也没有帮助。 stackoverflow.com/a/1206687/453673。也没有用空格替换它。
  • 考虑安装更新的 GCC,比如 4.8;它有很好的 C++2011 支持。
  • 谢谢。不过,问题是因为之前的程序员添加了一些额外的标记粘贴操作符;正如我在下面的答案中所示。

标签: c++ gcc ubuntu-12.04 template-meta-programming


【解决方案1】:

解决了。我浏览了this page 并了解了应该如何使用令牌粘贴运算符:

原来有一些额外的 ## 运算符被 Visual Studio 忽略,但 gcc 没有。 我更改的行是:

#define ACF_MAKE_PARAMS1_0(t)
#define ACF_MAKE_PARAMS1_1(t)    t ## 1
#define ACF_MAKE_PARAMS1_2(t)    t ## 1,  t ## 2
#define ACF_MAKE_PARAMS1_3(t)    t ## 1,  t ## 2,  t ## 3
#define ACF_MAKE_PARAMS1_4(t)    t ## 1,  t ## 2,  t ## 3,  t ## 4
#define ACF_MAKE_PARAMS1_5(t)    t ## 1,  t ## 2,  t ## 3,  t ## 4,  t ## 5
#define ACF_MAKE_PARAMS1_6(t)    t ## 1,  t ## 2,  t ## 3,  t ## 4,  t ## 5,  t ## 6

前面的行有类似##t##2 的内容,它正在创建错误。我将其更改为 t##2 并且有效!荣耀哈利路亚! :-) 模板元编程的折磨就这样结束了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多