以下代码输出:
struct Property<int>::IValue
但我希望它输出:
struct Property<int>::Value<int>
码:
struct IProperty
{
virtual ~IProperty() = 0;
};
IProperty::~IProperty(){}
template<typename T>
struct Property : IProperty
{
struct IValue
{
virtual ~IValue() = 0;
};
template<typename Q>
struct Value : IValue
{
Q thing;
};
Property() : m_pValue(new Value<T>()){}
std::shared_ptr<IValue> m_pValue;
};
template<typename T>
Property<T>::IValue::~IValue(){}
int main()
{
std::unique_ptr<IProperty> p(new Property<int>);
std::cout << typeid(*static_cast<Property<int>&>(*p).m_pValue).name() << '\n';
}
Run Code Online (Sandbox Code Playgroud)
如果IValue并且Value被移到外面Property以便它们不再是嵌套类,我得到了我期望的结果.这是VS Bug还是预期的行为?
我有一个ConcurrentQueue基于用户提供的容器的类,有一个像这样的构造函数...
ConcurrentQueue(const ConcurrentQueue& other) : m_Queue(other.m_Queue) {}
Run Code Online (Sandbox Code Playgroud)
但是,我需要other在复制时锁定互斥锁.
选项1:
所以我根本不能使用复制构造函数,并且......
ConcurrentQueue(const ConcurrentQueue& other) : m_Queue(other.m_Queue)
{
std::lock_guard<std::mutex> lock(other.m_Mutex);
m_Queue = other.m_Queue;
}
Run Code Online (Sandbox Code Playgroud)
但我不能保证复制分配和复制构造是等效的功能.
选项2:
我可以有私人方法......
std::queue<T, Container> GetQueue() const
{
std::lock_guard<std::mutex> lock(other.m_Mutex);
return m_Queue;
}
Run Code Online (Sandbox Code Playgroud)
然后在构造函数中执行此操作...
ConcurrentQueue(const ConcurrentQueue& other) : m_Queue(other.GetQueue()) {}
Run Code Online (Sandbox Code Playgroud)
但这可能(取决于优化)使用m_Queue的复制构造函数一次,并且它的移动构造函数一次.我也不能保证副本和移动只相当于副本.此外,用户提供的容器可能是奇怪的并且是可复制的但是不可移动的,这也会导致这种方法出现问题.
那么,我该怎么办?
找到2的幂的最快方法是什么,使用一定数量(即2的幂)?
我对数学不是很熟练,所以我不确定如何最好地描述它.但功能将类似于x = 2^y这里y是输出,并且x是输入.这是一个真实表,如果有助于解释它的外观.
0 = f(1)
1 = f(2)
2 = f(4)
3 = f(8)
...
8 = f(256)
9 = f(512)
Run Code Online (Sandbox Code Playgroud)
我已经做了一个这样做的功能,但我担心它不是很有效(或者说优雅).这样做会有更简单,更有效的方法吗?我正在使用它来计算纹理的哪个区域用于缓冲绘制的完成方式,因此每个绘制的对象至少调用一次.这是我到目前为止所做的功能:
uint32 getThePowerOfTwo(uint32 value){
for(uint32 n = 0; n < 32; ++n){
if(value <= (1 << n)){
return n;
}
}
return 32; // should never be called
}
Run Code Online (Sandbox Code Playgroud) struct S
{
int x;
int y;
};
std::atomic<S> asd{{1, 2}}; // what should this be? This doesn't work
Run Code Online (Sandbox Code Playgroud)
编辑:这两个{{1, 2}}和({1, 2})在G ++,在铛没有工作的工作.clang有解决方法吗?
c++ atomic aggregate-initialization uniform-initialization c++11
我有以下类型特征:
template <class T>
struct Arity : Arity<decltype(&T::operator())> {};
template <class T, class R, class... Args>
struct Arity<R(T::*)(Args...)> {
static constexpr auto value = sizeof...(Args);
};
template <class T, class R, class... Args>
struct Arity<R(T::*)(Args...) const> {
static constexpr auto value = sizeof...(Args);
};
template <class R, class... Args>
struct Arity<R(*)(Args...)> {
static constexpr auto value = sizeof...(Args);
};
Run Code Online (Sandbox Code Playgroud)
对于大多数用例来说,找到函数所使用的参数数量非常有用,但是对于一个常见情况它会失败:
auto l1 = [](int, double){};
Arity<decltype(l1)>::value; // works, 2
auto l2 = [](auto, auto){};
Arity<decltype(l2)>::value; // error: Reference to overloaded …Run Code Online (Sandbox Code Playgroud) 我正在看[VC10的] unique_ptr,他们做了一些我不明白的事情:
typedef typename tr1::remove_reference<_Dx>::type _Dx_noref;
_Dx_noref& get_deleter()
{ // return reference to deleter
return (_Mydel);
}
unique_ptr(pointer _Ptr,
typename _If<tr1::is_reference<_Dx>::value, _Dx,
const typename tr1::remove_reference<_Dx>::type&>::_Type _Dt)
: _Mybase(_Ptr, _Dt)
{ // construct with pointer and (maybe const) deleter&
}
typename tr1::add_reference<_Ty>::type operator*() const
{ // return reference to object
return (*this->_Myptr);
}
Run Code Online (Sandbox Code Playgroud)
不只是写_Dx&或_Ty&是同一回事?
我实际上明白他们为什么在这里做到了:
unique_ptr(pointer _Ptr, typename tr1::remove_reference<_Dx>::type&& _Dt)
: _Mybase(_Ptr, _STD move(_Dt))
{ // construct by moving deleter
}
Run Code Online (Sandbox Code Playgroud) 我还没有完全围绕C++ 11多线程的东西,但我试图让多个线程等到主线程上的某个事件然后一次继续(处理发生的事情),并wait再次当他们'完成处理......循环直到它们关闭.以下不完全是 - 这是我的问题的简单再现:
std::mutex mutex;
std::condition_variable cv;
std::thread thread1([&](){ std::unique_lock<std::mutex> lock(mutex); cv.wait(lock); std::cout << "GO1!\n"; });
std::thread thread2([&](){ std::unique_lock<std::mutex> lock(mutex); cv.wait(lock); std::cout << "GO2!\n"; });
cv.notify_all(); // Something happened - the threads can now process it
thread1.join();
thread2.join();
Run Code Online (Sandbox Code Playgroud)
这有效...除非我停在一些断点上并放慢速度.当我这样做,我看Go1!,然后挂断,等待thread2的cv.wait.怎么了?
也许我不应该使用条件变量......周围没有任何条件wait,也没有需要使用互斥锁保护的数据.我该怎么做呢?
std::vector<int> vec;
auto i = vec.begin(), j = std::next(i);
Run Code Online (Sandbox Code Playgroud)
错误:在声明者列表中,"auto"必须始终推导为相同的类型
发生了什么导致调试版本与未附加的版本相比,附加到调试器的速度要慢得多?它们都是相同的exe运行.
编辑:大多数答案都集中在断点上.我仍然像没有断点的泥浆,OutputDebugString或观察窗口中的任何东西一样运行.那么调试CRT,运行时堆栈检查和调试堆呢?
以下标准是否符合要求?你能举这个部分吗?
struct A
{
virtual void func() = 0;
};
struct B
{
void func(){}
};
struct C : public A, public B
{
virtual void func(){ B::func(); }
};
Run Code Online (Sandbox Code Playgroud)
我在VS2010中得到了一个奇怪的编译器警告,它在同等但更复杂的代码中指向func派生最多类中的声明:warning C4505: unreferenced local function has been removed.我不知道为什么编译器认为在类中声明的虚函数是本地的; 但是我不能在一个更简单的例子中重复这个警告.
我想出了一个小警告的重复案例.假设它与函数隐藏有关,我认为我走错了路.这是repro案例:
template<typename T>
struct C
{
int GetType() const;
virtual int func() const; // {return 4;} // Doing this inline removes the warning <--------------
};
template<typename T>
int C<T>::GetType() const
{
return 0;
}
template<>
int …Run Code Online (Sandbox Code Playgroud)