Visual C 的示例不合格的代码?

发布于 2024-10-15 04:18:00 字数 66 浏览 4 评论 0原文

使用 Visual C++ 时有哪些不符合标准的代码示例?允许在 Visual C++ 下编译的东西,但没有其他东西。

What are some examples of code that are not standards compliant when using visual C++? Something that is allowed to compile under visual C++ but nothing else.

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

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

发布评论

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

评论(6

篱下浅笙歌 2024-10-22 04:18:00

您可以在此处找到所有 Microsoft 语言扩展;您可能还想了解一下语言领域,其中VC++ 不符合标准。

我认为是标准的(当我启用 /Za switch) 是“神奇的左值转换”:

char *p;
(( int * ) p )++; 

You can find all the Microsoft language extensions here; you may also want to have a look to the areas of the language where VC++ is not compliant to the standard.

One that I thought was standard (I noticed it when I enabled the /Za switch) is the "magic l-value cast":

char *p;
(( int * ) p )++; 
居里长安 2024-10-22 04:18:00

Visual C++ 的某些版本接受将非常量引用传递给临时对象。像这样的东西:

void DoSomething(std::string& str);

void NonConformantFunction()
{
    DoSomething("Temporary std::string created here");
}

Some version of Visual C++ accept to pass non-const reference to temporary object. Something like that :

void DoSomething(std::string& str);

void NonConformantFunction()
{
    DoSomething("Temporary std::string created here");
}
守不住的情 2024-10-22 04:18:00

“允许编译的东西
在 Visual C++ 下,但没有其他”

“不符合标准的代码
合规”

并不描述完全相同的事情。编译器可能完全兼容标准,同时具有该编译器独有的扩展,而不兼容是标准明确禁止的事情。还有许多“未定义”或ISO 标准的“实现定义”部分可能会阻止可移植性而不不兼容。此外,许多受支持的扩展都受到其他编译器的支持,因此这是您的约束之一的示例,但不是另一个约束

。导致其代码不可移植的 VC++ 扩展是所有 C++/CLI 扩展,因此也是需要它们的 .NET Framework 类库。

"Something that is allowed to compile
under visual C++ but nothing else"

and

"code that are not standards
compliant"

do not describe exactly the same thing. A compiler may be fully standard compliant while having extensions that are unique to that compiler, while a non-compliance is something explicitly prohibited by the standard. There is also a number of "undefined" or "implementation defined" parts of the ISO standard that might prevent portability without being non-compliant. Moreover many supported extensions are supported by other compilers so are examples of one of your constraints but not the other.

Now that said, the major extension of VC++ that would render its code non-portable are all of the C++/CLI extensions, and therefore also the .NET Framework class library which requires them.

哽咽笑 2024-10-22 04:18:00

microsoft.com 上有一个 官方页面 说明了哪些部分VC++ 与标准不兼容。然而,另一个问题是它默认与标准兼容。例如,for变量的默认范围是仍然错误VC++2010

There is an official page on microsoft.com saying what are the parts where VC++ is not compatible with the standard. However a different issue is where it is compatible by default with the standard. For example the default scope for for variables is still wrong in VC++2010.

赢得她心 2024-10-22 04:18:00

我没有 VC 编译器来测试这个,但如果我没记错的话,无论注释的错误如何,它都可以在 Visual Studio 中正常编译:

template <typename T>
struct base {
   void foo() {
      T::type v = 0;    // standard requires typename here
      std::cout << v << std::endl;
   }
};
template <typename T>
struct derived : base<T>
{
   void bar() {
      foo();            // foo() is not dependent this should not compile   
   }
};
struct test {
   typedef int type;
};
int main() {
   derived<test> o;
   o.bar();
}

I don't have a VC compiler to test this, but if I recall correctly this will compile fine in Visual Studio regardless of the commented errors:

template <typename T>
struct base {
   void foo() {
      T::type v = 0;    // standard requires typename here
      std::cout << v << std::endl;
   }
};
template <typename T>
struct derived : base<T>
{
   void bar() {
      foo();            // foo() is not dependent this should not compile   
   }
};
struct test {
   typedef int type;
};
int main() {
   derived<test> o;
   o.bar();
}
往事风中埋 2024-10-22 04:18:00

MSVC++ 允许您做的一件事是在类中显式地专门化模板。例如。

class X {
public:
    template <typename T> void doStuff(T value);

    template <> void doStuff<bool>(bool value) {
        // ..do something specific to bool.
    }
};

这在 VS 中编译得很好,但尝试在 GCC 中编译会给出错误,告诉您在非命名空间范围中有显式专业化。解决办法就是简单地把专业化拉长。

class X {
public:
    template <typename T> void doStuff(T value);
};

template <> void X::doStuff<bool>(bool value) {
    // ..do something specific to bool.
}

不过,根据规范,GCC 在这个问题上是正确的,规范规定所有显式专业化都应该在命名空间范围内。

可能值得注意的是,在后一种情况下,您必须在头文件中定义您的专业化,而不是像您通常期望的那样在实现文件中定义。提到的两个编译器都不符合解决此问题的标准,即在实现文件中的专门化上声明的 export 关键字。不过,大多数编译器并未实现此功能,并且计划从下一版本的规范中删除它。

One thing MSVC++ allows you to do is to explicitly specialize templates within a class. Eg.

class X {
public:
    template <typename T> void doStuff(T value);

    template <> void doStuff<bool>(bool value) {
        // ..do something specific to bool.
    }
};

This compiles fine in VS, but attempting to compile in GCC will give an error telling you that you have an explicit specialization in the non-namespace scope. The solution to which is to simply drag out the specialization.

class X {
public:
    template <typename T> void doStuff(T value);
};

template <> void X::doStuff<bool>(bool value) {
    // ..do something specific to bool.
}

GCC is correct on this issue though, according to the spec, which states that all explicit specializations should be in the namespace scope.

Possibly worth noting that in the latter case, you must define your specialization within the header file, and not the implementation file like you would normally expect. Both the compilers mentioned are non compliant with the standard which would solve this problem, which is the export keyword declared on the specialization within an implementation file. This feature is not implemented by most compilers though, and there are plans to remove it from the next version of the specification.

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