小编c.b*_*ear的帖子

Visual Studio C++编译器奇怪的行为

我只是想知道为什么这小段代码在Visual Studio中正确编译(并且没有警告).也许结果与GCCClang相同,但不幸的是我现在无法测试它们.

struct T {
    int t;
    T() : t(0) {}
};

int main() {
    T(i_do_not_exist);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ most-vexing-parse visual-studio-2012

36
推荐指数
2
解决办法
1158
查看次数

继承类中的比较运算符

我已经定义了一个IntWrapper类如下的类:

struct IntWrapper
{
protected:
  int value;

public:
  explicit IntWrapper() = default;
  explicit IntWrapper(const int value) : value(value) {}

  bool operator< (const IntWrapper rhs) const { return value <  rhs.value; }
  bool operator> (const IntWrapper rhs) const { return value >  rhs.value; }
  bool operator<=(const IntWrapper rhs) const { return value <= rhs.value; }
  bool operator>=(const IntWrapper rhs) const { return value >= rhs.value; }
  bool operator==(const IntWrapper rhs) const { return value == rhs.value; }

  explicit operator …
Run Code Online (Sandbox Code Playgroud)

c++ c++11

15
推荐指数
2
解决办法
894
查看次数

std::unordered_set 指针

我有以下结构

struct MyClass {
    int myInt;
    std::map<int, int> myMap;
};
Run Code Online (Sandbox Code Playgroud)

我想使用unordered_set<MyClass*, PointedObjHash, PointedObEq>,但找不到有效的方法来声明PointedObEq.

我试过

struct PointedObjHash {
    size_t operator() (MyClass* const& c) const {
        std::size_t seed = 0;
        boost::hash_combine(seed, c->myInt);
        boost::hash_combine(seed, c->myMap);
        return seed;
    }
Run Code Online (Sandbox Code Playgroud)

我希望一切都好,但我找不到一种方式来声明PointedObjEq

- - 编辑 - -

如果在类调试中声明operator==永远不会中断,但我认为因为MyClass == MyClass*永远不会发生......

struct MyClass {
    ...
    ...
    bool operator==(MyClass* const& c) {
        return this->myInt == c->myInt & this->myMap == c->myMap;
    }
Run Code Online (Sandbox Code Playgroud)

c++ hash set

5
推荐指数
1
解决办法
8178
查看次数

SQL Server模糊查询验证

我刚刚遇到了一个好奇的SQL Server行为.

在我的场景中,我有一种动态数据库,所以我需要在运行涉及它们的查询之前检查的存在.

我无法解释为什么查询

IF 0 = 1 -- Check if NotExistingTable exists in my database
BEGIN
    SELECT NotExistingColumn FROM NotExistingTable
END
GO
Run Code Online (Sandbox Code Playgroud)

正确执行,但查询

IF 0 = 1 -- Check if NotExistingColumn exists in my ExistingTable
BEGIN
    SELECT NotExistingColumn FROM ExistingTable
END
GO
Run Code Online (Sandbox Code Playgroud)

返回无效的列名称'NotExistingColumn'.

在这两种情况下,IF块都不会执行并包含无效的查询(第一个错过一个表,第二个错过一个列).

SQL引擎在一种情况下检查语法错误是否有任何原因?

提前致谢

sql-server

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

c ++内联汇编优化

为什么Visual Studio C++编译器默认情况下不会优化以下代码?

#include "ctime"
#include "iostream"

#define BIG_NUM 10000000000

int main() {

    std::clock_t begin = clock();

    for (unsigned long long i = 0; i < BIG_NUM; ++i) {
         __asm
        {
            nop
        }
    }

    std::clock_t end = clock();
    std::cout << "time: " << double(end - begin) / CLOCKS_PER_SEC;

    std::cin.get();
}
Run Code Online (Sandbox Code Playgroud)

如果没有_asm块,则操作时间始终为0,因为由于编译器优化,完全"跳过"循环.使用该_asm块只需几秒钟.

是否有任何编译器标志来优化内联汇编或由于某些不明原因而无法实现?

c++ inline-assembly

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