小编Vik*_*ehr的帖子

Java是否提供某种Register \"Cookies"来存储临时数据?

Java是否提供了某种注册表或cookie机制,我可以存储小块数据以加载下一次启动我启动java应用程序或applet?

例如,应用程序设置,如上次打开的文件等

java registry cookies applet

4
推荐指数
2
解决办法
398
查看次数

const decltype(*std :: begin(container))&val不会使val const?

这段代码:

std::vector <int> ints(5,1);
std::for_each(ints.begin(), ints.end(), [](const decltype(*std::begin(ints))& val){ val*=2; });
Run Code Online (Sandbox Code Playgroud)

在Visual Studio 2010中编译并运行得很好,并修改容器中的每个值,就好像const关键字不存在一样.这是编译器中的错误,因为预期的行为是val是不可修改的吗?(换句话说,我希望它不会编译,但确实如此)

更新:

std::for_each(ints.begin(), ints.end(), [](const std::remove_reference<decltype(*std::begin(ints))>::type& val){ val*=2; });
Run Code Online (Sandbox Code Playgroud)

似乎行为正常,但这并不能让我更聪明.

注意:

decltype(*std::begin(ints)) 是对int的引用.

c++ lambda const visual-c++ c++11

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

如何在不更改代码的情况下强制在Visual C++中强制POD类型的值初始化?

有没有办法强制0\false\nullptrVisual C++发布模式下初始化pod类型?

更具体地说,我不想更改我的代码,只需将其编译为初始化为的pod类型 0\false\nullptr.

我想要这个,因为我希望我们的系统是确定性的.

c++ msbuild pod visual-studio-2010

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

在移动构造函数之前调用了析构函数?

我有一个看起来像这样的函数,它通过movesmanmantics返回一个不可复制的类:

MyClass&& MyFunction() {
  MyClass myClass;
  do some stuff;
  return std::move(myClass);
}
Run Code Online (Sandbox Code Playgroud)

然后由

main() {
  MyClass myClass = MyFunction();
}
Run Code Online (Sandbox Code Playgroud)

该类boost::noncopyable用于防止复制。它具有构造函数,移动构造函数和移动分配。我的问题是,析构函数在move构造函数之前被调用。我做错了什么?

c++ visual-studio-2010 move-semantics c++11

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

std :: string_view编译时哈希

似乎C++ 17 string_view 的std :: hash函数不是constexpr.

在我看来,绑定到const char []的字符串视图可以在编译时进行散列(这将是非常甜蜜的),或者有什么可以防止这种情况?

c++ hash std string-view c++17

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

批处理文件字符串操作

然而,这是一个非常具体的问题;

假设我有一个从位于目录中的\运行的批处理文件 c:\data\src\branch_1

如何将我的环境变量设置%buildpath%c:\build\bin\branch_1一个批处理文件?

(要清楚一点,如果c:\foo\bar\branch_2我想要将相同的批处理文件设置%buildpath%c:\build\bin\branch_2)

string build-process batch-file environment-variables

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

那么,如今基础类如何在C++ 11中编写?

更新:我使用MSVC10,它没有给我默认的移动语义

假设我想创建一个带有几个非pod成员的常规类;

class Foo {
NonPodTypeA a_;
NonPodTypeB b_;
}
Run Code Online (Sandbox Code Playgroud)

像往常一样,我实现了一个复制构造函数,以及一个使用copy-constructor的赋值运算符:

Foo(const Foo& other) : a_(other.a_), b_(other.b_) {}
Foo& operator=(const Foo& other) { 
  Foo constructed(other);
  *this = std::move(constructed);
  return *this;
}
Run Code Online (Sandbox Code Playgroud)

然后我实现了move-constructor和move-assignment,它使用std :: swap代替std :: move为所有成员,因为它们可能在move-semantics可用之前编写,因为move-semantics已实现,我可以省略实现交换成员函数:

Foo(Foo&& other) {
  ::std::swap(a_, other._a);
  ::std::swap(b_, other._b);
}
Foo& operator=(Foo&& other) { 
  ::std::swap(a_, other._a);
  ::std::swap(b_, other._b);
  return *this;
}
Run Code Online (Sandbox Code Playgroud)

这就是我的问题; 假设我对会员一无所知,可以在这里做些更通用的事吗?

例如,move-constructor与const声明的成员不兼容,但如果我实现移动构造函数,因为Foo(Foo&& other) : a_(std::move(other.a_)), b_(std::move(other.b_)){}我不能确保没有复制移动语义的类?我可以以一种聪明的方式在移动分配中使用移动构造函数吗?

c++ class-design rvalue move-semantics c++11

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

显式int类型作为参数

是否可以编写一个函数:

void func(uint64_t val) {...}
Run Code Online (Sandbox Code Playgroud)

如果使用任何其他整数类型调用uint64_t,而不修改我的#pragma警告,则会生成编译时错误?

即:

uint32_t x = 0;
func(x) {...} // Error!
func(uint64_t(x)) {...} // Succes!
Run Code Online (Sandbox Code Playgroud)

c++ type-conversion

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

C++ foreach-loop选择mutable begin()甚至

写作时

for(const auto& val: my_container)
  sum += val
Run Code Online (Sandbox Code Playgroud)

Visual Studio选择了可变版本begin(),这是设计还是错误?

当我在写容器上使用副本时,这在我的代码中是一个非常严重的性能问题.

c++ foreach c++11 visual-studio-2012

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

为什么 C++ 标准库不使用可能/不可能的属性?

在 Visual Studio 中阅读标准库算法的实现时,他们根本不使用[[likely]]/属性。[[unlikely]]对我来说,应该使用的教科书示例[[unlikely]]是例如std::find_if(...),微软已经这样实现:

// Note that some noise are removed from the code
template <class _InIt, class _Pr>
_InIt find_if(_InIt _First, const _InIt _Last, _Pr _Pred) {
    for (; _First != _Last; ++_First) {
        if (_Pred(*_First)) {
            break;
        }
    }
    return _First;
}
Run Code Online (Sandbox Code Playgroud)

如前所述,find_if(...)这是 if 子句的 true 分支不太可能的教科书示例,因为大多数情况下它会在谓词验证为 true 之前迭代几个元素,因此 [[unlikely]] 将是一个优化机会。Microsoft 没有[[unlikely]]在这里使用该属性有什么原因吗?

c++ algorithm micro-optimization compiler-optimization likely-unlikely

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