为什么带有模板参数包指针的模板函数无法使用相同指针的偏移量进行实例化?
我的意思是:给出这个简短的代码为什么我必须注释掉最后两行?
template <int * ... pt> void f() {}
int n[] = {1, 2, 3};
int m = n[1];
int main()
{
f<n>(); // this is accepted
f<n, &m>(); // this is accepted too
//f<n, n+1>(); // this is not.
//f<n, &n[1]>(); // this isn't accepted neither
}
Run Code Online (Sandbox Code Playgroud)
并不n+1代表相同的地址&m?或者链接有什么不同?还有什么?
当谈到使用CAS 循环std::atomic实现时,此链接中的 cppreference给出了以下示例push:
template<typename T>
class stack
{
std::atomic<node<T>*> head;
public:
void push(const T& data)
{
node<T>* new_node = new node<T>(data);
new_node->next = head.load(std::memory_order_relaxed);
while(!head.compare_exchange_weak(new_node->next, new_node,
std::memory_order_release,
std::memory_order_relaxed /* Eh? */));
}
};
Run Code Online (Sandbox Code Playgroud)
现在,我不明白如何将 comestd::memory_order_relaxed用于失败情况,因为据我了解, (与-strongcompare_exchange_weak相同,但为了方便起见,我只是使用弱版本)是失败时的加载操作,这意味着它从另一个线程中成功的 CAS 操作加载,因此它应该使用同步…?std::memory_order_releasestd::memory_order_acquire
while(!head.compare_exchange_weak(new_node->next, new_node,
std::memory_order_release,
std::memory_order_acquire /* There you go! */));
Run Code Online (Sandbox Code Playgroud)
假设,如果“宽松负载”获得旧值之一,最终一次又一次失败,并在循环中停留额外的时间怎么办?
下面这张粗糙的图片是我的大脑被卡住的地方。
T2 的商店在 T1 上不应该可见吗?(通过相互之间具有同步关系)
总结一下我的问题,
std::memory_order_acquire,而不是std::memory_order_relaxed失败呢?std::memory_order_relaxed足够呢?std::memory_order_relaxed失败是否意味着 …我对刚刚添加到最新C++标准中的新函数std :: move()感到有些好奇.
读完一篇关于它的文章,发现该函数的定义是
namespace std {
template <class T>
inline typename remove_reference<T>::type&& move(T&& x)
{
return x;
}
}
Run Code Online (Sandbox Code Playgroud)
这似乎在调用std :: move和使用强制转换之间没有任何区别.
比如这里,
class NPC{
int m_number1;
int m_number2;
public:
NPC() : m_number1(1), m_number2(2) {
cout << "NPC Constructor called!" << endl;
}
NPC(NPC&& _npc) : m_number1(_npc.m_number1), m_number2(_npc.m_number2) {
_npc.m_number1 = 0;
_npc.m_number2 = 0;
cout << "NPC Move Constructor called!" << endl;
}
};
int main() {
NPC n1;
NPC n2 = std::move(n1);
cout << "----------------" << endl; …Run Code Online (Sandbox Code Playgroud) 如你所知,_Remove_reference存在,将T&转换为T或T &&转换为T.
我以一种俏皮的心情制作了以下代码,它完全不像我预期的那样工作,但不知道为什么.
template<class _Ty>
struct _Remove_reference
{ // remove reference
typedef _Ty _Type;
static void func(){ cout << "1" << endl; }
};
// template<class _Ty>
// struct _Remove_reference<_Ty&>
// { // remove reference
// typedef _Ty _Type;
// static void func(){ cout << "2" << endl; }
// };
//
// template<class _Ty>
// struct _Remove_reference<_Ty&&>
// { // remove rvalue reference
// typedef _Ty _Type;
// static void func(){ cout << "3" << endl; }
// }; …Run Code Online (Sandbox Code Playgroud) 我最近看过GoingNative2012当然是关于C++ 11.
在Bjarne Stroustrup部分,我发现有一个有趣的运算符函数,如下所示.
constexpr Value<Second> operator""s(long double d)
{
return Value<Second>(d);
}
Run Code Online (Sandbox Code Playgroud)
好吧,除了在C++ 11中看起来像新关键字的constexpr之外,
我从来不知道""是否可以超载?
这是C++中的新功能之一,虽然我没能使用VS 2010测试它吗?
提前致谢.
上次我找到了很多关于SFINAE的答案,建议使用void_t帮助器.
但我似乎并不明白它在以下方面有什么特别之处:
decltype (..., void()).
Run Code Online (Sandbox Code Playgroud)
考虑这个例子:
template <typename...>
using void_t = void;
template <typename T, typename = void>
struct has_foo : std::false_type {};
template <typename T>
struct has_foo <T, decltype (T().foo(), void())> : std::true_type {};
template <typename T, typename = void>
struct has_bar : std::false_type {};
template <typename T>
struct has_bar <T, void_t <decltype (T().bar())> > : std::true_type {};
class MyClass1
{
public:
int foo() { return 3; }
};
class MyClass2
{
public:
double bar() { return …Run Code Online (Sandbox Code Playgroud) 我不太明白为什么在使用VC2010使用调试模式构建时不会调用此复制构造函数.
class SomeClass
{
public:
SomeClass(int meaningless){}
SomeClass(const SomeClass& sc)
{
cout << "Copy Constructor invoked!" << endl;
}
};
int main()
{
SomeClass test(SomeClass(9999)); // Copy constructor not invoked.
}
Run Code Online (Sandbox Code Playgroud)
我认为这与RVO无关,因为我没有返回任何值.
更有趣的是,当我将复制构造函数设为私有时,即使编译器省略了复制构造函数,编译器也不会编译.
当我试图设置IsDefault每个敷料项目的属性匹配条件时,它会抛出错误说:
序列包含多个匹配序列.
(this.DressingItems
.Where(xx => xx.DressingInfo.CatID == catId
&& xx.ProductID == this.ProductID)
.Single()).IsDefault = false;
Run Code Online (Sandbox Code Playgroud) 请参阅以下经典生产者 - 消费者代码:
int main()
{
std::queue<int> produced_nums;
std::mutex m;
std::condition_variable cond_var;
bool done = false;
bool notified = false;
std::thread producer([&]() {
for (int i = 0; i < 5; ++i) {
std::this_thread::sleep_for(std::chrono::seconds(1));
std::unique_lock<std::mutex> lock(m);
std::cout << "producing " << i << '\n';
produced_nums.push(i);
notified = true;
cond_var.notify_one();
}
done = true;
cond_var.notify_one();
});
std::thread consumer([&]() {
std::unique_lock<std::mutex> lock(m);
while (!done) {
while (!notified) { // loop to avoid spurious wakeups
cond_var.wait(lock);
}
while (!produced_nums.empty()) {
std::cout << "consuming …Run Code Online (Sandbox Code Playgroud) 我创建了一个类,它具有std::initializer_list使用VS2013支持的相同实现Uniform Initialization.
template <class _Elem>
class My_Initializer_List { ... } // Hope to replace std::initializer_list!
Run Code Online (Sandbox Code Playgroud)
我注意到的第一件事是编译器一直在寻找std::initializer_list什么时候它有替代它,这是My_Initiliazer_List.
void Foo()
{
// The compiler tries to invoke
// its constructor with std::initializer<int>{ 10, 20 }.
auto MyVar = My_Initializer_List<int>{ 10, 20 }; // Compile Error!
}
Run Code Online (Sandbox Code Playgroud)
如果此功能依赖于C++标准库,那么我相信我应该能够将其替换为我自己的类,至少在学术上是这样.
提前致谢.
c++ ×9
c++11 ×5
c# ×1
concurrency ×1
constructor ×1
copy ×1
linq-to-sql ×1
locking ×1
memory-model ×1
sfinae ×1
stdatomic ×1
templates ×1