C++模板化函数和前向声明

发布于 2024-09-17 12:39:53 字数 1289 浏览 12 评论 0原文

我正在编写一些使用 MSVC 在 Windows 上编译和链接(甚至已经发布了商业产品)的代码。但它不能用 GCC 编译,我收到以下错误:

.../CBaseValue.h: In member function 'bool CBaseValue::InstanceOf()':
.../CBaseValue.h:90:18: error: invalid use of incomplete type 'struct CValueType'
.../CBaseValue.h:11:7: error: forward declaration of 'struct CValueType'

CBaseValue.h

class CValueType;

class CBaseValue {
public:

...

    template <typename _Type>
    bool InstanceOf() {
        CValueType* pType = GetType();
        if(pType == NULL) {
            return false;
        }
        else {
            return pType->IsDerivedFrom<_Type>();
        }
    }

...

}

CValueType.h

class CValueType : public CBaseValue  {
public:

...

    template <typename _Type>
    bool IsDerivedFrom() {
        return IsDerivedFrom(_Type::TYPEDATA);
    }

...

}

我明白为什么这是一个问题。基类 (CBaseValue) 有一个使用派生类(在本例中为 CValueType)的模板化函数。

看起来 MSVC 并没有完全遵守 C++ 规范,而我刚刚被它咬住了。但是,在调用模板化函数的代码实际编译之前使用前向声明的 MSVC 行为现在也更可取。有谁知道一种解决方法,可以让此代码与 GCC 一起使用,而无需重写大量基本代码?

根据我自己的研究,看起来将“-fno-implicit-templates”传递给 g++ 会有所帮助,但随后我需要显式定义调用的模板类型。它们有很多,所以如果我能避免的话,我会更喜欢它。如果普遍共识是这是我最好的选择……那就这样吧!

如果有人想知道,我正在将代码移植到 Mac,这就是我们现在使用 GCC 的原因。

I'm working on some code that compiles and links (and even has released commercial products) on Windows using MSVC. It doesn't compile with GCC though, I get the following errors:

.../CBaseValue.h: In member function 'bool CBaseValue::InstanceOf()':
.../CBaseValue.h:90:18: error: invalid use of incomplete type 'struct CValueType'
.../CBaseValue.h:11:7: error: forward declaration of 'struct CValueType'

CBaseValue.h

class CValueType;

class CBaseValue {
public:

...

    template <typename _Type>
    bool InstanceOf() {
        CValueType* pType = GetType();
        if(pType == NULL) {
            return false;
        }
        else {
            return pType->IsDerivedFrom<_Type>();
        }
    }

...

}

CValueType.h

class CValueType : public CBaseValue  {
public:

...

    template <typename _Type>
    bool IsDerivedFrom() {
        return IsDerivedFrom(_Type::TYPEDATA);
    }

...

}

I understand why this is a problem. The base class (CBaseValue) has a templated function that uses a derived class (in this case CValueType).

It looks like MSVC isn't exactly obeying the C++ spec here and I've just been bitten by it. But the MSVC behavior of using the forward declaration until code calling the templated function is actually compiled is also more desirable right now. Does anybody know of a work-around where I can get this code working with GCC without having to rewrite a lot of base code?

From my own research it looks like passing '-fno-implicit-templates' to g++ would help but then I'd need to explicitly define the called template types. There are a lot of them so if I can avoid that I'd prefer it. If the general consensus is that this is my best option... so be it!

And in case anybody is wondering, I'm porting the code over to the Mac which is why we're now using GCC.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

等风也等你 2024-09-24 12:39:54

该标准的格式不正确,但不需要诊断。 MSVC 可以不诊断这个特殊情况(即使发生实例化!)。

更具体地说,(C++03) 标准规则为 14.6/7

如果非依赖名称中使用的类型在定义模板时不完整,但在完成实例化时完整,并且该类型的完整性是否影响程序格式良好或影响程序的语义,则程序格式错误;无需诊断。

因此,解决方案是仅使类型相关,但安排在实例化期间指定该类型。例如,您可以通过像这样重写模板来做到这一点

template<typename T, typename> // just ignore second param!
struct make_dependent { typedef T type; };

template <typename Type> // eww, don't use "_Type" in user code
bool InstanceOf() {
    typename make_dependent<CValueType, Type>::type* pType = GetType();
    // ...
        return pType->template IsDerivedFrom<Type>();
    // ...
}

This is ill-formed by the Standard, but no diagnostic is required. MSVC is fine not diagnosing this particular case (even when instantiation happens!).

More specifically, the (C++03) Standard rules at 14.6/7

If a type used in a non-dependent name is incomplete at the point at which a template is defined but is complete at the point at which an instantiation is done, and if the completeness of that type affects whether or not the program is well-formed or affects the semantics of the program, the program is ill-formed; no diagnostic is required.

So the solution is to just make the type dependent, but arrange it that during instantiation, that type is designated. For example, you can do that by rewriting your template like this

template<typename T, typename> // just ignore second param!
struct make_dependent { typedef T type; };

template <typename Type> // eww, don't use "_Type" in user code
bool InstanceOf() {
    typename make_dependent<CValueType, Type>::type* pType = GetType();
    // ...
        return pType->template IsDerivedFrom<Type>();
    // ...
}
你的心境我的脸 2024-09-24 12:39:54

似乎 CBaseValue::InstanceOf() 函数对于不包括 CValueType.h 的任何人来说都是无用的。

因此,请等到所有需要的类型都可用后再提供定义。 (编辑:这正是 Charles Bailey 在我打字时发表的评论所建议的——我想我们的想法是一样的。)

CBaseValue.h

class CValueType;

class CBaseValue {
public:

...

    template <typename _Type>
    bool InstanceOf();

...

}

CValueType.h

class CValueType : public CBaseValue  {
public:

...

    template <typename T>
    bool IsDerivedFrom() {
        return IsDerivedFrom(T::TYPEDATA);
    }

...

}


template <typename T>
inline bool CBaseValue::InstanceOf() {
        CValueType* pType = GetType();
        if(pType == NULL) {
            return false;
        }
        else {
            return pType->IsDerivedFrom<T>();
        }
    }

不过,它们看起来非常紧密地耦合,所以可能只有一个头文件两个类或一个以正确顺序包含各个标头的公共标头文件会更好。

It seems that the CBaseValue::InstanceOf() function is useless to anyone not including CValueType.h.

So wait to provide the definition until all needed types are available. (EDIT: This is exactly what is suggested by Charles Bailey's comment that he posted while I was typing -- I guess we think alike.)

CBaseValue.h

class CValueType;

class CBaseValue {
public:

...

    template <typename _Type>
    bool InstanceOf();

...

}

CValueType.h

class CValueType : public CBaseValue  {
public:

...

    template <typename T>
    bool IsDerivedFrom() {
        return IsDerivedFrom(T::TYPEDATA);
    }

...

}


template <typename T>
inline bool CBaseValue::InstanceOf() {
        CValueType* pType = GetType();
        if(pType == NULL) {
            return false;
        }
        else {
            return pType->IsDerivedFrom<T>();
        }
    }

They seem very tightly coupled though, so maybe having just one header file for both classes, or one public header file that includes the individual headers in the correct order, would be better.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文