使用结构标识符来表示POD类型和C结构 [英] Use of struct identifier to signify POD types and C structures

查看:239
本文介绍了使用结构标识符来表示POD类型和C结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑下面的代码:

  struct Foo {}; 

template< typename ForwardIterator>
struct Foo *
Bar(ForwardIterator first,ForwardIterator last)
{
(void)first;
(void)last;
Foo * foo(nullptr);
return foo;
}

上面的代码在 Clangv3.5 GCCv4.9



但是,它无法在VC ++ 2013中编译。



删除 struct 标识符(见下文)从返回类型解决问题:

  struct Foo { }; 

template< typename ForwardIterator>
Foo *
Bar(ForwardIterator first,ForwardIterator last)
{
(void)first;
(void)last;
Foo * foo(nullptr);
return foo;
}



Q1:



这是一个视觉化的工作室错误吗?



Q2:


$ b b

这个问题出现了,因为在我的代码中, Foo struct $ c> .hc 文件(即,是一个C struct ),以表示C / POD struct s在我的代码,我使用 struct 标识符。这是一个坏主意(即,在C ++代码中,我应该避免以这种方式使用 struct 标识符)?

解决方案

详细说明的类型说明符的主要目的是允许您引用隐藏的名称,从草稿C ++标准部分 3.4.4 详细说明类型说明符


详细说明类型说明符(7.1.6.3)可用于引用
以前声明的类名或枚举名称,即使名称有
被非类型声明(3.3.10)隐藏。


,因此在隐藏 Foo 的情况下,您必须使用详细说明的类型说明符: p>

  struct Foo {}; 

void Foo()
{
}

我在中看不到任何内容7.1.6.3 精心设计的类型说明符 14 会阻止这种使用。实际上,它似乎来自 CS2989 的说明。Visual Studio正在变得混乱并认为你正在尝试重新定义一个非模板类作为模板类。



所以这看起来像一个bug,所以我会提交一个错误报告



更新 / p>

提交了错误报告


Consider the following piece of code:

struct Foo {};

template<typename ForwardIterator>
struct Foo*
Bar(ForwardIterator first, ForwardIterator last)
{ 
    (void)first;
    (void)last;
    Foo *foo(nullptr);
    return foo;
}

The above piece of code compiles fine in Clangv3.5 and GCCv4.9.

However, it fails to compile in VC++2013.

Removing the struct identifier (see below) from the return type solves the problem:

struct Foo {};

template<typename ForwardIterator>
Foo*
Bar(ForwardIterator first, ForwardIterator last)
{ 
    (void)first;
    (void)last;
    Foo *foo(nullptr);
    return foo;
}

Q1:

Is this a visual studio bug?

Q2:

This issue came up, because in my code-base the Foo struct lies in a .h.c file (i.e., is a C struct) and in order to signify C/POD structs in my code, I use the struct identifier. Is this a bad idea (i.e., in C++ code I should avoid to use struct identifier in this fashion)?

解决方案

The main point of an elaborated type specifiers is to allow you refer to a name that has been hidden, from the draft C++ standard section 3.4.4 Elaborated type specifiers:

An elaborated-type-specifier (7.1.6.3) may be used to refer to a previously declared class-name or enum-name even though the name has been hidden by a non-type declaration (3.3.10).

and so in the case where Foo is hidden, you would have to use an elaborated type specifiers:

struct Foo {};

void Foo()
{
}

I see nothing in 7.1.6.3 Elaborated type specifiers or 14 Templates that would prevent this use. In fact it looks like from the description of CS2989 Visual Studio is getting confused and thinks you are attempting to redefine a non-template class as a template class.

So this looks like a bug to me, so I would file a bug report.

Update

Filed a bug report.

这篇关于使用结构标识符来表示POD类型和C结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆