小编Gam*_*Gam的帖子

如何阻止VS代码强制我使用大括号,否则光标位置变得疯狂(PHP,或全局应用)

VS代码

if (1 == 1)  // When I hit enter here, the cursor moves to the next line at pos (0)
|
else  
    return; // When I hit enter here, cursor moves to next line/ pos 4
    |       // why is the cursor here? why not pos 0? 
Run Code Online (Sandbox Code Playgroud)

Visual Studio(这就是我想要发生的事情)

if (1 == 1)   // When I hit enter here, the cursor moves to the next line at
    |         // pos 1 if I'm working with Tabs or pos 4 …
Run Code Online (Sandbox Code Playgroud)

visual-studio-code vscode-extensions

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

如果密钥已存在,则将unique_ptr插入到地图中而不删除指针的有效方法

简单的方法是显而易见的

std::map<int,std::unique_ptr<something>> mymap;

auto f = mymap.find(5);
std::unique_ptr<something> myptr;

if (f == mymap.end())
    mymap.insert({5, std::move(myptr)});
Run Code Online (Sandbox Code Playgroud)

但是,这看起来效率不高,因为我必须在地图中找到两次键.一个用于检查密钥是否不存在,并且插入功能也将执行相同的操作.

如果我只是使用mymap.insert({5, std::move(myptr)});那么我的唯一ptr(myptr)如果pair.second返回false(键已经存在)就消失了.

编辑:

显然答案是在C++ 17上try_emplace,并且它已经在我正在使用的编译器中可用(vs2015),而且由于我正在处理个人项目,我可以负担得起它.

c++ c++17

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

尝试catch语法构造函数

http://ideone.com/UtVzxw

struct base
{
    base() { throw std::exception(); }
};

struct derived : public base
{
    derived() try : base()  { }

    catch (std::exception& e)
    {
        std::cout << "exception handled" << std::endl;
    }
};

int main()
{
    derived a; // My app crashes.
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

不是我的应用程序写"异常处理"并继续运行?

我发现的唯一解决方案是a在try/catch块中包围" " 的构造.但是,如果我这样做,那么首先在构造函数中使用try/catch有什么意义呢?我猜也许它的用途是清理可能已分配的成员变量?既然没有调用析构函数

以下工作,但处理异常2次.

struct base
{
    base() { throw std::exception(); }
};

struct derived : public base
{
    derived() try : base() { }

    catch(std::exception& e)
    {
      std::cout …
Run Code Online (Sandbox Code Playgroud)

c++ exception

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

调试器友好的零成本命名引用数组中的特定位置

struct A
{
    int a   = 1;
    short b = 2;
    char c  = 3;
}

struct B
{
    using arr_type = array<A,3>;
    char asd = 0;
    A a1;  
    A a2;
    A a3;

    // is this safe to use to loop trough all 3 elements?
    arr_type* p1 = reinterpret_cast<arr_type*>(&a1);

    // or maybe this one?
    A* p2 = &a1;
};
Run Code Online (Sandbox Code Playgroud)

我可以安全地使用p1p2循环播放a1...a3吗?

B b;

for (int i = 0; i < 3; i++)
{
     cout << …
Run Code Online (Sandbox Code Playgroud)

c++ c++14

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

我应该尝试避免静态同步方法

根据我的理解,以下代码效率不高:

class Foo {
    static Resource resource1;
    static Resource resource2;

    static synchronized void methodA() {
       resource1.add("abc");
    }

    static synchronized void methodB() {
       resource2.add("abc");
    }
}
Run Code Online (Sandbox Code Playgroud)

根据我的理解,两种方法都锁定在一个对象(类对象Foo.class)中,所以我猜测以下是一个很好的优化?

class Foo {
    static Resource resource1;
    static Resource resource2;

    static void methodA()  {
       synchronized(resource1) {
           resource1.add("abc");
       }
    }

    static void methodB()  {
       synchronized(resource2) {
           resource2.add("123");
       }
    }
}
Run Code Online (Sandbox Code Playgroud)

只要这两种资源不相互依赖.

我什么时候应该考虑使用静态同步方法?

java

6
推荐指数
2
解决办法
126
查看次数

我允许用memcpy(*this)修改构造函数中的所有类成员吗?

struct Something
{
    int a;
    int b;

    Something(char* buffer)
    {
        memcpy(this, buffer, sizeof(Something));
    };
};
Run Code Online (Sandbox Code Playgroud)

这合法吗?安全?对我来说它看起来很好,但我不确定C++标准是否以某种方式禁止它.

c++

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

启用 c++17 智能感知打开文件夹visual studio ninja-clang

使用“ -Xclang -std=c++17”我可以构建可执行文件,但是我找不到激活 c++17 智能感知的内容。我已经尝试了很多组合,如下所示,但似乎都不起作用

CMakeLists.txt

cmake_minimum_required(VERSION 3.9.2)
set(CMAKE_CXX_STANDARD 17)
project(myapp)
add_compile_options("-Xclang" "-std=c++17")
add_executable(myapp main.cpp)
set_target_properties(myapp PROPERTIES CXX_STANDARD 17)
target_compile_features(myapp PRIVATE cxx_std_17)
Run Code Online (Sandbox Code Playgroud)

主程序

#include <tuple>
namespace test1::test2 // red [qualified name is not allowed]
//       ^^^^^^^^^^^^^
{}

int main()
{
    auto[a, b] = std::pair<int, int>();
    //  ^^^^^^
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

CMakeSettings.json

{
  // See https://go.microsoft.com//fwlink//?linkid=834763 for more information about this file.
  "configurations": [
    {
      "name": "x64-Debug",
      "generator": "Ninja",
      "configurationType": "Debug",
      "inheritEnvironments": [ "msvc_x64_x64" ],
      "buildRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}\\build\\${name}",
      "installRoot": "${env.USERPROFILE}\\CMakeBuilds\\${workspaceHash}\\install\\${name}",
      "cmakeCommandArgs": …
Run Code Online (Sandbox Code Playgroud)

c++ cmake visual-studio-2017

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

线程安全的局部变量

void HelloWorld()
{
   static std::atomic<short> static_counter = 0;
   short val = ++static_counter; // or val = static_counter++;
}
Run Code Online (Sandbox Code Playgroud)

如果从两个线程调用此函数,

局部变量val可以1在两个线程中吗?或(如果static_counter++使用,则为0 ?)

c++ thread-safety c++14

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

startActivty在另一个线程中

我对android很新.希望这不是一个愚蠢的问题.

protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    new Thread(new Runnable()
    {
        public void run()
        {
            Intent intent = new Intent(MainActivity.this, SecondActivity.class);
            startActivity(intent);
        }
    }).start();
}
Run Code Online (Sandbox Code Playgroud)

问题:为什么这有效?从辅助线程调用startActivity是正常的吗?

我认为所有与UI相关的事情都必须在UI线程中完成.

android

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

我应该使用哪个容器

假设我想拥有一个价格不同的苹果容器.我希望它们按价格排序(最高价格优先),但我也希望通过他们的ID快速检索它们.到目前为止我所拥有的是以下内容

struct AppleClass
{
    string id;
    int price;

    bool operator<(const AppleClass& o) const 
    {
        return price > o.price; 
    }
};

int main() 
{
    set<AppleClass> myapples;
    myapples.insert({"apple1", 500});
    myapples.insert({"apple2", 600});
    myapples.insert({"apple3", 400});

    for (auto& apple : myapples)
    {
        cout << apple.id << "," << apple.price << endl;         
    }
}
Run Code Online (Sandbox Code Playgroud)

http://ideone.com/NcjWDZ

我的应用程序将花费20%的时间删除条目,20%插入条目,25%检索它们(检索整个列表),35%更新它们(它们的价格将增加或减少)经常.

容器最多可包含450个条目.

我的代码只解决了排序问题.查找是无用的,因为我想通过他们的id找到(所以我需要迭代所有这些).出于同样的原因,删除和插入会很慢.

这感觉是错误的选择.

但是,如果我有一张地图,那么它将根据id进行排序.每次我检索列表时,我都必须将其复制到某个容器中,例如,订购它,然后将其发送给用户,这也感觉很慢.

救命!

c++

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

在初始化之前使用局部变量

// bind function: 
template<typename T> T bind(T& v) 
{   
    // Can I toy with the object v refers to? or that's undefined behaviour?
    // the object v refers to is not initialized yet, But the object has been allocated, so I can use that memory, I think?

    // The following 2 lines should be fine if I'm correct.
    // Which is my function is currently is doing (sorta)
    myvector.emplace_back(SQLType<T>(), (void*)&v);
    return 0;
}

SomeClass value = bind(value);
Run Code Online (Sandbox Code Playgroud)

我想知道我是否可以value在初始化之前使用该对象(当 …

c++

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