这是一个非常简单的问题.请考虑以下代码:
#include <iostream>
#include <memory>
typedef std::unique_ptr<void> UniqueVoidPtr;
int main() {
UniqueVoidPtr p(new int);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
使用以下命令使用cygwin(g ++ 4.5.3)进行编译g++ -std=c++0x -o prog file.cpp就可以了.但是,使用Microsoft编译器(VS 2010或2013)进行编译时出现此错误:
C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\INCLUDE\memory(2067) : error C2070: 'void': illegal sizeof operand
C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\INCLUDE\memory(2066) : while compiling class template member function 'void std::default_delete<_Ty>::operator ()(_Ty *) const'
with
[
_Ty=void
]
C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\INCLUDE\type_traits(650) : see reference to class template instantiation 'std::default_delete<_Ty>' being compiled
with
[ …Run Code Online (Sandbox Code Playgroud) 我有一个应用程序,其中一些STL容器在3个线程中读取,并用2写入.我知道有多线程容器的TBB,但它不是我的应用程序中的一个选项.
所以我想使用std :: mutex和我的双手使程序线程安全.这是我所做的简单版本:
int readers = 0;
std::mutex write;
// One write, no reads.
void write_fun()
{
write.lock();// We lock the resource
while(readers > 0){}// We wait till everyone finishes read.
// DO WRITE
write.unlock();// Release
}
// Multiple reads, no write
void read_fun()
{
// We wait if it is being written.
while(!write.try_lock()){}
write.unlock();
readers++;
// do read
readers--;
}
Run Code Online (Sandbox Code Playgroud)
这是在C++ 11中执行此操作的正确方法吗?
是否有一个标准的实现for_each,它调用元素和范围中的下一个元素?
例如,取范围{0, 1, 2, 3, 4, 5},我想f用每个元素及其后继者调用一个函数:{f(0, 1), f(1, 2), f(2, 3), f(3, 4), f(4, 5)}
注意最后一个元素是如何被省略的,因为它没有后继元素.
如果对n个后继者进行泛化,那么它也会很好.
到目前为止,我总是用迭代器的手写循环来解决这个问题.但是,我想更多地遵循C++ 11范围的基础或std::for_each避免锅炉板代码.
// today: handwritten loop
for(Range::iterator current = range.begin(); current != range.end(); ++current)
f(*current, *std::next(current));
// near future: wrapped version
for_each_pair(range.begin(), range.end(), f);
// slightly further future: generalized version
for_each_tuple<n>(range.begin(), range.end(), f);
Run Code Online (Sandbox Code Playgroud)
可以改进函数的名称.对我来说for_each_pair/ tuple听起来应该返回范围大小为n的所有子集(这本身就是我想要解决的另一个问题).所以我想对更好的名字提出一些建议:
for_each_adjacent<n>
Run Code Online (Sandbox Code Playgroud) 我想只查看RGB图像中的R + G通道,因为当蓝色通道被移除时,我可以更好地对比检测对象.我使用OpenCV来分割通道,但是在将蓝色通道设置为0之后合并它时,我的代码无法编译.
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main( int argc, char** argv )
{
if( argc != 2)
{
cout <<" Usage: display_image ImageToLoadAndDisplay" << endl;
return -1;
}
Mat image,fin_img;
image = imread(argv[1], CV_LOAD_IMAGE_COLOR); // Read the file
if(! image.data ) // Check for invalid input
{
cout << "Could not open or find the image" << std::endl ;
return -1;
}
namedWindow( "Display window", CV_WINDOW_AUTOSIZE );// Create a window …Run Code Online (Sandbox Code Playgroud) 我想知道我是否可以使用由明确指定的单一类型组成的参数包.例如,像这样:
#include <iostream>
using namespace std;
void show() { }
template<typename First, typename... Rest>
void show(First f, Rest... rest)
{
cout << f << endl;
show(rest...);
}
void foo(int f, int... args) // error
{
show(f, args...);
}
int main()
{
foo(1, 2, 3);
}
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是定义foo().使用OS X clang ++版本5(llvm 3.3svn)我收到错误error: type 'int' of function parameter pack does not contain any unexpanded parameter packs.
当然,我可以通过更改foo()为函数模板来编译它:
template<typename... Args>
void foo(int f, Args... args)
{
show(f, args...); …Run Code Online (Sandbox Code Playgroud) 我偶然发现了这个问题:我似乎无法在正常的索引位置选择项目std::set.这是性病的一个错误吗?
下面一个简单的例子:
#include <iostream>
#include <set>
int main()
{
std::set<int> my_set;
my_set.insert(0x4A);
my_set.insert(0x4F);
my_set.insert(0x4B);
my_set.insert(0x45);
for (std::set<int>::iterator it=my_set.begin(); it!=my_set.end(); ++it)
std::cout << ' ' << char(*it); // ups the ordering
//int x = my_set[0]; // this causes a crash!
}
Run Code Online (Sandbox Code Playgroud)
我能做些什么来解决这个问题?
Stackoverflow还有关于此问题的另一个问题,但我没有找到可接受的解决方案.所以我再问一遍,因为旧的问题没有引起人们的注意.
情况就是这样.我有'main.qml','feature1.qml','feature2.qml'定义的应用程序屏幕.
这些屏幕共享标题栏下方的相同工具栏.工具栏有多个项目,因此复制粘贴QML代码就像疯了一样.这个问题:QML文件包含 - 或者一个单片文件(结构QML代码)?说可以只使用QML文件名作为组件名称,但我无法使其正常工作.
有解决方案吗 详情请参阅.
我读到当我们有指针成员和定义基类时需要定义析构函数,但我不确定我是否完全理解.我不确定的一件事是,定义默认构造函数是否无用,因为默认情况下我们总是给出一个默认构造函数.另外,我不确定是否需要定义默认构造函数来实现RAII原则(我们只需要将资源分配放在构造函数中而不是定义任何析构函数吗?).
class A
{
public:
~Account()
{
delete [] brandname;
delete b;
//do we need to define it?
};
something(){} =0; //virtual function (reason #1: base class)
private:
char *brandname; //c-style string, which is a pointer member (reason #2: has a pointer member)
B* b; //instance of class B, which is a pointer member (reason #2)
vector<B*> vec; //what about this?
}
class B: public A
{
public something()
{
cout << "nothing" << endl;
}
//in all other cases …Run Code Online (Sandbox Code Playgroud) 为什么std::lock_guard不可移动,它会使代码更好:
auto locked = lock_guard(mutex);
Run Code Online (Sandbox Code Playgroud)
代替
std::lock_guard<std::mutex> locked(mutex);
Run Code Online (Sandbox Code Playgroud)
创建自己的版本有什么问题,例如:
template <typename T> class lock_guard_
{
T* Mutex_;
lock_guard_(const lock_guard_&) = delete;
lock_guard_& operator=(const lock_guard_&) = delete;
public:
lock_guard_(T& mutex) : Mutex_(&mutex)
{
Mutex_->lock();
}
~lock_guard_()
{
if(Mutex_!=nullptr)
Mutex_->unlock();
}
lock_guard_(lock_guard_&& guard)
{
Mutex_ = guard.Mutex_;
guard.Mutex_ = nullptr;
}
};
template <typename T> lock_guard_<T> lock_guard(T& mutex)
{
return lock_guard_<T>(mutex);
}
Run Code Online (Sandbox Code Playgroud)
?
任何根本原因让它变得可移动是个坏主意吗?
根据这个问题的答案,以下代码是合法的:
#define three 3
#define nine three*3
int main()
{
std::cout << nine;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当然,它编译并运行良好.但是,上述问题的答案还指出,应该注意这些#define指令的顺序,并且#define应该在它们之前定义将在其他s中使用的指令.但是以下代码:
#define nine three*3
#define three 3
int main()
{
std::cout << nine;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编译并运行良好,并打印"9".
我的编译器是否让我轻松,或者订单确实与使用其他#defines的#defines无关?编译是否会在更复杂的项目中失败?
值得一提的是,上述问题涉及C,而我的代码是C++.这是(假设的)行为差异的来源吗?