我知道C++ 11增加了对线程的支持.例如:
#include <iostream>
#include <thread>
void bar()
{
std::cout << "bar()\n";
}
int main()
{
std::thread thread(bar);
thread.join();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但是,有没有办法bar在单独的进程中执行该功能?如果没有,是否有关于是否应该添加此功能的讨论?
注意:我知道使用独立于平台的库的可能性,我只是好奇C++是否直接支持或将来支持.
程序A产生编译错误(按预期),因为isFinite使用非整数类型调用.
#include <iostream>
class Foo {};
template<typename T>
bool isFinite(const T& t)
{
static_assert(std::is_integral<T>::value, "Called isFinite with a non-integral type");
return false;
}
int main()
{
Foo f;
std::cout << "Foo is finite? " << ((isFinite(f)) ? "yes" : "no") << "\n";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但是,稍作修改(参见程序B)允许程序编译(Visual Studio 2013)并生成以下输出.
Foo is finite? yes
#include <iostream>
class Foo {};
template<typename T>
bool isFinite(const T& t)
{
static_assert(std::is_integral<T>::value, "Called isFinite …Run Code Online (Sandbox Code Playgroud) 我在这里找到了一些关于Null对象模式的信息(https://softwareengineering.stackexchange.com/questions/152094/null-pointers-vs-null-object-pattern)和这里(http://en.wikipedia.org/ wiki/Null_Object_pattern#C.2B.2B).
但是,C++实现并没有说明我的用例.
我还看到了Nullable Type的相关链接(http://en.wikipedia.org/wiki/Nullable_type).
我有一个不属于层次结构的对象,通常不会在堆上分配.此外,没有一个方便的值可以用作指示null的标记.希望以下代码使用例清晰.
class ContrivedType
{
public:
ContrivedType() :
mValue(0)
{
// Do nothing
}
bool operator==(const ContrivedType& other) const
{
return mValue == other.mValue;
}
void setValue(std::uint16_t value)
{
mValue = value;
}
private:
// All values in the range [0, 65535] are valid for use
std::uint16_t mValue;
};
class Foo
{
public:
const ContrivedType getValue() const
{
return mValue;
}
void …Run Code Online (Sandbox Code Playgroud) 通常我会看到= default标题中使用的语法.我的理解是,这与在标题中明确实现函数的情况相同,见Foo下文.
#pragma once
class Foo
{
public:
Foo() = default;
Foo(const Foo& other) = default;
};
Run Code Online (Sandbox Code Playgroud)
纯粹出于好奇,可以= default在源文件中使用如下吗?
#pragma once
class Bar
{
public:
Bar();
Bar(const Bar& other);
};
Run Code Online (Sandbox Code Playgroud)
#include "Bar.h"
Bar::Bar() = default;
Bar::Bar(const Bar&) = default;
Run Code Online (Sandbox Code Playgroud)
据我所知,这相当于在cpp文件中显式实现这些功能.
上面的Bar例子编译gcc-5.1但是标准是否允许这种用法?
顺便说一下,= default在源文件和标题中使用是否有任何好处?
我目前正在尝试优化一些占用50%时间的代码std::pow().我知道指数将始终是一个正整数,并且基数将始终是区间(0,1)中的双精度.为了好玩,我写了一个函数:
inline double int_pow(double base, int exponent)
{
double out = 1.0;
for(int i = 0; i < exponent; i++)
{
out *= base;
}
return out;
}
Run Code Online (Sandbox Code Playgroud)
我正在编译:
> g++ fast-pow.cpp -O3 --std=c++11
Run Code Online (Sandbox Code Playgroud)
我在(0,1)之间产生了1亿个双打,并比较了(1) std::pow(2)int_pow上面我自制函数的时间和(3)直接乘法.这是我的计时例程的草图(这是一个非常快速的组合测试):
void time_me(int exp, size_t reps)
{
volatile double foo = 0.0;
double base = 0.0;
size_t i;
for (i = 0; i < reps; ++i)
{
base = ((double) rand() / (RAND_MAX)) + 1;
foo = pow(base, …Run Code Online (Sandbox Code Playgroud) 我看到 C++11 文档 ( http://en.cppreference.com/w/cpp/language/lambda ) 中的 lambda 表达式声明支持按值捕获和引用,但不支持右值引用。我能找到的与此相关的最接近的问题是:How to capture a unique_ptr into a lambda expression? ,但我的用例似乎不需要使用std::bind.
#include <iostream>
#include <memory>
class Foo
{
public:
explicit Foo(int value = 0) : mValue(value) {}
// The following items are provided just to be explicit
Foo(Foo &&other) = default;
Foo &operator=(Foo &&other) = default;
Foo(const Foo &other) = delete;
Foo &operator=(const Foo &other) = delete;
~Foo() {}
int mValue;
};
void bar(std::unique_ptr<Foo> f)
{
std::cout << "bar: …Run Code Online (Sandbox Code Playgroud) 我知道以下问题:C++ 11 lambdas:成员变量捕获陷阱.此外,我知道需要通过捕获this指针捕获类成员,因为这个问题的答案清楚地说明了.
是.捕获成员变量总是通过捕获它来完成; 它是访问成员变量的唯一方法.
但是,捕获this指针会捕获所有类成员.是否可以限制捕获哪些班级成员?例如,是否可以捕获单个类成员?
我知道以下不起作用但是有可能实现吗?
class Foo
{
public:
Foo() : mBar1(1), mBar2(2) {}
void doBar()
{
auto test = [this->mBar1]()
{
std::cout << mBar1 << "\n";
// Trying to access 'mBar2' here would fail to compile...
};
test();
}
int mBar1;
int mBar2;
};
Run Code Online (Sandbox Code Playgroud)
来自评论:
你为什么需要这个?
我不需要这样做.我只是想知道这是否可行,如果可行,怎么做.
#include <iostream>
#include <type_traits>
struct Foo
{
// ### Member Function ###
void bar() { std::cout << "Foo::bar()\n"; }
};
// ### 1 Parameter Primary Template (which is empty) ###
template<typename>
struct Traits {};
// ### 2 Parameter Template Specialization ###
template<class T, class U>
struct Traits<T U::*>
^^^^^^ // I don't understand this syntax
{
using type1 = T;
using type2 = U;
};
int main()
{
// ### Pointer to member function ###
void (Foo::*memFuncPtr)() = &Foo::bar;
Foo …Run Code Online (Sandbox Code Playgroud) 所以我们假设我有以下课程
class NoDefaultConstructor {
NoDefaultConstructor() = delete;
...
};
Run Code Online (Sandbox Code Playgroud)
我有另一个类,有一个类型NoDefaultConstructor和其他成员的数组
class Wrapper {
std::array<NoDefaultConstructor, 2> arr;
...
};
Run Code Online (Sandbox Code Playgroud)
如何在构造函数中初始化数组Wrapper(可能在初始化列表中使用std::intializer_list)?
更具体地说,是我可以将参数传递给初始化列表中的数组构造函数Wrapper以获得类似于以下的构造的唯一方法吗?我正在考虑这样做,因为数组的大小将来可能会改变.
template <typename... Values>
Wrapper(Values&&... values) : arr{std::forward<Values>(values)...} {}
Run Code Online (Sandbox Code Playgroud) 我一直在考虑为我的 C++ 项目编写 static_if ,我偶然发现了以下代码:
#include <iostream>
using namespace std;
namespace static_if_detail {
struct identity {
template<typename T>
T operator()(T&& x) const {
return std::forward<T>(x);
}
};
template<bool Cond>
struct statement {
template<typename F>
void then(const F& f){
f(identity());
}
template<typename F>
void else_(const F&){}
};
template<>
struct statement<false> {
template<typename F>
void then(const F&){}
template<typename F>
void else_(const F& f){
f(identity());
}
};
} //end of namespace static_if_detail
template<bool Cond, typename F>
static_if_detail::statement<Cond> static_if(F const& f){
static_if_detail::statement<Cond> if_;
if_.then(f); …Run Code Online (Sandbox Code Playgroud) 我收到错误"错误:无效使用AppleFarmer::AppleFarmer.我不知道为什么我收到此错误,因为我没有尝试将任何输入传递给我的构造函数.是否有可能我的.h文件有问题?什么是我做错了得到这个错误?我有三个不同的文件,我可能也遇到了将代码链接在一起的问题,因为我正在#include为.cpp文件做.我不确定我的代码是否与此错误有关,但我坚持这个错误.
appleFarmerMain.cpp
#include<iostream>
#include "appleFarmer.cpp"
int main(){
AppleFarmer m;
int harvest;
int demand;
m.AppleFarmer();
while(m.endOfMonth()==false){
cout<<"Enter a harvest amount:"<<endl;
cin>>harvest;
m.harvestApples(harvest);
cout<<"Enter a demand:"<<endl;
cin>>demand;
m.sellApples(demand);
cout<<"Apple Inventory: "<<m.getInventory()<<endl;
m.updateCurrentDay();
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
appleFarmer.cpp
#include "appleFarmer.h"
#include "<iostream>
using namespace std;
AppleFarmer::AppleFarmer(){
for(int i=0;i<30;i++){
sales[i]=0;
harvest[i]=0;
}
}
bool AppleFarmer::sellApples(int demand){
if(demand<= inventory){
sales[currentDay]=demand;
inventory=inventory-demand;
}
else{
sales[currentDay]=0;
}
}
void AppleFarmer::harvestApples(int dayHarvest){
harvest[currentDay]= dayHarvest;
inventory=inventory+dayHarvest;
}
bool AppleFarmer::endOfMonth(){
if (currentDay=maxDays){
return true;
}
else{ …Run Code Online (Sandbox Code Playgroud) 我的第一个理解是cout指向终端的输出,但我不知道这是否正确.
也许它是一些内存地址,它不是终端的输出,而是连接到它.
我很困惑,任何澄清都会非常感激.
编辑:
当我这样做std::cout << std::cout;输出是0x467f84,它是一个地址,对吧?
在我的课程中,我需要动态地分配一个卡列表,但是这段代码有一行我不确定.
if (newPlayer)
{
cout << "Enter your name: ";
cin >> playerName;
newPlayer->nom = playerName;
newPlayer->nextPlayer= NULL;
newPlayer->cardsInHand= NULL;
if (playersList)
{
//this for I dont understand. How can a for loop become this and what does it does
for (p = playersList; p->nextPlayer; p = p->nextPlayer);
p->nextPlayer= newPlayer;
}
else
{
playerList= newPlayer;
newPlayer->nextPlayer= NULL;
}
}
Run Code Online (Sandbox Code Playgroud) c++ ×13
c++11 ×6
templates ×2
arrays ×1
class ×1
constructor ×1
cout ×1
for-loop ×1
oop ×1
performance ×1