用它std::ignore来忽略未使用的变量是一种好方法吗?
假设我有这样的函数:
void func(int i)
{
//for some reason, I don't need i anymore but I cannot change signature of function
std::ignore = i;
}
Run Code Online (Sandbox Code Playgroud)
附加信息
这是一个例子,一些答案建议使用匿名变量.但是我如何为其他情况做这件事,比如:
int Thread_UnSafe_func_returnSomething():
void func()
{
// To make it thread safe
// Also it is required to call only once
static int i = Thread_UnSafe_func_returnSomething();
std::ignore = i;
}
Run Code Online (Sandbox Code Playgroud) 我有一个基类和派生类的层次结构。基类具有一个虚函数,该虚函数被派生类覆盖。
class Base
{
public:
~Base();
virtual void other_functionality() = 0;
};
class Derived : public Base
{
public:
~Derived ();
void other_functionality() {//some code};
};
Run Code Online (Sandbox Code Playgroud)
现在,如果我这样做:
int main()
{
Base * P = new Derived ();
delete p;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它给出错误:
删除具有非虚拟析构函数的多态类类型的对象。
但是使用unique_ptr,它会通过而不会发出警告。
int main()
{
std::unique_ptr<Base> p;
p.reset(new Derived ());
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我知道我是否使用虚拟析构函数。用裸露的指针警告将得到解决。但是问题仍然存在-为什么没有虚拟析构函数会显示裸指针而不是unique_ptr问题。
假设有这样的函数:
int * func()
{
std::unique_ptr<int> ptr(new int(3));
//Few more lines of code
//then one function added where programmer writes like some thing
SOME_OTHER_FUNC(std::move(ptr));
return ptr.get();
}
void SOME_OTHER_FUNC(std::unique_ptr<int> arg_ptr)
{
}
Run Code Online (Sandbox Code Playgroud)
有没有办法警告程序员避免这样的错误std::move?这unique_ptr不仅仅是针对其他对象而已.
当我们不恰当地使用移动对象时,是否有任何机制来生成警告?
我知道了future返回的原因std::async有一些特殊的共享状态,通过它wait on returned future发生在未来的析构函数中。但是当我们使用 时std::pakaged_task,它的未来不会表现出相同的行为。为了完成任务打包,你必须显式调用get()上future的对象packaged_task。
现在我的问题是:
std::async与std::packaged_task)的内部实现可能是什么?futurereturn from std::packaged_task?或者,换句话说,相同的行为是如何停止的std::packaged_task future?要查看上下文,请查看以下代码:
它不会等待完成countdown任务。但是,如果我取消评论// int value = ret.get();,它就会结束countdown并且很明显,因为我们实际上是在阻止返回的未来。
// packaged_task example
#include <iostream> // std::cout
#include <future> // std::packaged_task, std::future
#include <chrono> // std::chrono::seconds
#include <thread> // std::thread, std::this_thread::sleep_for
// count down taking a second for each value:
int countdown (int from, …Run Code Online (Sandbox Code Playgroud) 我写了以下程序
#include <iostream>
#include <stdexcept>
class Myclass
{
public:
~Myclass()
{
//throw std::runtime_error("second (in destructor)");
throw 1;
}
};
void fun()
{
Myclass obj;
}
int main()
{
try
{
fun();
}
catch (const std::exception& e)
{
std::cout << e.what();
}
catch(...)
{
std::cout << " ... default Catch" << std::endl;
}
std::cout << "Normal" << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我在C++98模式(cpp.sh)上运行程序时,它会打印出来
... default Catch
Normal
Run Code Online (Sandbox Code Playgroud)
当我使用C++14模式运行它时,它不会打印任何内容. 为什么这种行为有变化?
我确实理解,每当发生任何异常并且任何destructor(在堆栈展开过程中)抛出任何异常时,它都会终止应用程序.但是这里只有一次异常从try块中抛出destructor.
#include <iostream>
using namespace std;
class NoConstructOperation
{
protected:
NoConstructOperation() = default;
virtual ~NoConstructOperation() = default;
public:
NoConstructOperation(const NoConstructOperation&) = delete;
NoConstructOperation& operator =(NoConstructOperation&) = delete;
NoConstructOperation(NoConstructOperation&&) = delete;
NoConstructOperation& operator = (NoConstructOperation&&) = delete;
};
class Myclass:public NoConstructOperation
{
};
int main() {
static_assert(!std::is_trivially_destructible<Myclass>::value, "Compiler provided destructor is public: Please declare it private");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如果我不继承Myclass与NoConstructOperation上面的代码给出了静态断言编译错误.
但是如果我Myclass使用NoConstructOperation is_trivially_destructiblecheck 继承不起作用,即使Myclass构造函数是公共的.这段代码编译,是什么原因?
在以下代码中:
class Myclass
{
public:
Myclass() = default;
~Myclass() = default;
Myclass(Myclass&&) = default;
Myclass& operator=(Myclass&&) = default;
Myclass(const Myclass&) = delete;
Myclass& operator=(const Myclass&) = delete;
};
Myclass GetObj()
{
Myclass obj;
return obj;
}
Myclass WrapperOfGetObj()
{
Myclass&& Wrapobj = GetObj();
return Wrapobj;
}
int main()
{
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当编译它给在功能上的错误WrapperOfGetObj上return Wrapobj.错误是
'Myclass :: Myclass(const Myclass&)':尝试引用已删除的函数
这意味着它正在尝试调用deleted复制构造函数.据我所知Wrapobj是左值,当返回它的行为应该是相同obj的GetObj方法,即,调用移动构造函数在时return.
那么为什么要寻找复制构造函数呢?我在这里错过了什么?
在32位计算机中,一个内存读取周期获取4 bytes个数据。
因此要读取下面的缓冲区,读取下面提到的128个缓冲区应该花费32个读周期bytes。
char buffer[128];
Run Code Online (Sandbox Code Playgroud)
现在,假设如果我按如下所述对齐了该缓冲区,那么请让我知道它将如何使其更快地读取?
alignas(128) char buffer[128];
Run Code Online (Sandbox Code Playgroud)
我假设内存读取周期将仅保留4个字节。
但是当我替换__LINE__为__FUNCTION__. 宏连接字符串文字“ __FUNCTION__”而不是实际的函数名称。
#include <iostream>
using namespace std;
namespace GB
{
class Test
{
public:
Test() { cout << "Constructor is executed\n"; }
~Test() {
cout << i << " " << "Destructor is executed\n";
this->i = 7;
}
int i = -1;
};
}
int main()
{
// Test(); // Explicit call to constructor
GB::Test t; // local object
t.i = 6;
t.~Test(); // Explicit call to destructor
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出
Constructor is executed
6 Destructor is executed
6 Destructor is …Run Code Online (Sandbox Code Playgroud) 我写我在哪里复制的钥匙的功能map,set,unordered_map,unordered_set的vector,现在我想添加一个编译时断言,以得到清晰的错误,如果有的试图通过一个vector,list在该功能.
template <typename container>
auto CopyKeyToVector(conatiner c)
{
//static assert to check c is map, unordered map only?
}
Run Code Online (Sandbox Code Playgroud)
任何想法,我们该怎么办但─因为map,unordered_map本身模板化的容器
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <map>
template <typename T>
auto Copy(T c)
{
std::vector<decltype(c.begin()->first)> lc;
//Copying
return lc;
}
int main()
{
std::map<int, int> map;
Copy(map);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在上面的代码我尝试vector从键的数据类型声明一个map但是我得到以下错误 -
"The C++ Standard forbids containers of const elements allocator<const T> is ill-formed."
Run Code Online (Sandbox Code Playgroud) 我有两个从类继承的类 Base
struct Base
{
virtual ~Base() = default;
};
class Derived_1:public Base
{
public:
void getSomeFunc();
};
class Derived_2:public Base
{
public:
void getSomeFunc();
};
Run Code Online (Sandbox Code Playgroud)
现在我想写一个函数,它接受基类指针对象并在动态转换后找到合适的孩子并返回它,以便我们可以调用正确版本的 getSomeFunc()
我试着写
auto getChild(Base* ptr)
{
Derived_1 * p = dynamic_cast<Derived_1*>(ptr)
if(p)
{
return p;
}
else
{
Derived_2 * q = dynamic_cast<Derived_2*>(ptr)
if(q)
{
return q;
}
else
{
// some handling to avoid bugs!!
}
}
Run Code Online (Sandbox Code Playgroud)
但它没有被编译。任何方式来满足我的要求。
编辑 1 ----------------------------------
编译器的错误是 - incorrect deduction of 'auto'。编译是gcc
c++ ×10
c++14 ×10
c++11 ×7
alignas ×1
auto ×1
c ×1
c++98 ×1
compile-time ×1
decltype ×1
destructor ×1
exception ×1
function ×1
gcc-warning ×1
macros ×1
optimization ×1
principles ×1
std-future ×1
stdasync ×1
visual-c++ ×1