标签: msvc14

这个程序中的16位数学是否会调用未定义的行为?

有一天,我升级我的Windows从MSVC2013到MSVC2017和LO构建环境,不料,在我的计划已被多年工作正常功能(现在仍然工作在克细++ /铛)突然开始时MSVC2017编译给出不正确的结果.

我能够重写函数再次给出正确的结果,但是经验让我好奇 - 我的函数调用了未定义的行为(直到现在恰好给出了正确的结果),或者是代码定义明确且MSVC2017正在越野车?

下面是一个简单的程序,在重写之前和之后都显示了该功能的玩具版本.特别是,当使用值为-32762的参数调用时,函数maybe_invokes_undefined_behavior(),如下所示,调用未定义的行为吗?

#include <stdio.h>

enum {ciFirstToken = -32768};

// This function sometimes gives unexpected results under MSVC2017
void maybe_invokes_undefined_behavior(short token)
{
   if (token >= 0) return;

   token -= ciFirstToken;  // does this invoke undefined behavior if (token==-32762) and (ciFirstToken==-32768)?
   if (token == 6)
   {
      printf("Token is 6, as expected (unexpected behavior not reproduced)\n");
   }
   else
   {
      printf("Token should now be 6, but it's actually %i\n", (int) token);  // under MSVC2017 this prints -65530 !?
   }
} …
Run Code Online (Sandbox Code Playgroud)

c++ undefined-behavior language-lawyer msvc14

8
推荐指数
1
解决办法
317
查看次数

从MSVC 2013(C ++ 11)迁移到MSVC 2019(C ++ 17)时std :: vector :: insert()停止工作

我目前正在从迁移大型代码库

  • Visual Studio 2013(v120)
  • C ++ 11

  • Visual Studio 2019(v142)
  • C ++ 17

现在,我的测试在奇怪的地方失败了-我的索引超出了崩溃范围,并且行为发生了其他奇怪的变化。进行挖掘时,我注意到以下代码:

#include <iostream>
#include <vector>

int main()
{
    std::vector<std::vector<int>> nestedVector;
    nestedVector.insert(nestedVector.begin(), {});
    std::cout << nestedVector.size() << " elements";
}
Run Code Online (Sandbox Code Playgroud)

在VS2013中生成一个单元素向量,在VS2019中生成一个空向量

其他插入方式

nestedVector.insert(nestedVector.begin(), std::vector<int>{});
nestedVector.insert(nestedVector.begin(), std::vector<int>());
Run Code Online (Sandbox Code Playgroud)

在这两种设置中均可正常工作,并正确添加新元素。这是怎么回事

c++ visual-studio visual-c++ c++17 msvc14

7
推荐指数
1
解决办法
129
查看次数

包含中的C ++“未定义”模块关键字

我想在Visual Studio中使用C ++ 20模块,但是我的一个项目使用的是Magick ++,它在“ magick ++。h”中定义了struct一个char*名为“ module” 的成员:

typedef struct _MagickInfo
{
  char
    *name,
    *description,
    *version,
    *mime_type,
    *note,
    *module; /* offending member here */

  /* ... other members ... */
} MagickInfo;
Run Code Online (Sandbox Code Playgroud)

我可以告诉编译器不要在特定的#include中处理“模块”吗?

c++ visual-studio c++20 c++-modules msvc14

3
推荐指数
1
解决办法
75
查看次数

方法存在检查器代码在vs2015中断

以下代码检查类A中是否存在foo()方法.此代码在vs2013下编译,但静态断言在vs2015上失败.哪个编译器版本说实话?如果vs2015,那么如何修复代码?

#include <type_traits>

struct MethodTester_foo {
    template<typename U, typename MethodType>
    static auto test(U* p) -> decltype(static_cast<MethodType>(U::foo));
    template<typename U, typename MethodType> static auto test(...)->std::false_type;
};

template <typename Class, typename MethodType, class MethodTester>
using HasMethod =
typename std::conditional
<
    std::is_same<
        decltype(MethodTester::template test<Class, MethodType>(0)),
        std::false_type
    >::value,
    std::false_type, std::true_type
>::type;

struct A { int foo() { return 1; } };

static_assert(HasMethod<A, int(A::*)(), MethodTester_foo>::value, "Has no method named foo");
Run Code Online (Sandbox Code Playgroud)

c++ c++11 msvc12 visual-studio-2015 msvc14

0
推荐指数
1
解决办法
38
查看次数