好的,所以我试着这样做
int b;
char x = 'a';
//Case 1
b = static_cast<int>(x);
std::cout<<"B is : "<<b<<std::endl;
//Case 2
b = *(int*)&x;
std::cout<<"B is changed as :: "<< b <<std::endl;
Run Code Online (Sandbox Code Playgroud)
现在我知道在第2种情况下,第一个字节x被重新解释为认为它是一个整数并且位模式被复制到b其中给出了一些垃圾,而在情况1中它只是将值转换char为int.
除此之外,这两者有什么不同吗?
在 boost 库中是否有boost-hana的替代方法,它可以让我创建类似的东西
typedef boost::AlterinativeToHana::map< make_pair<"abcd",ABCDType>,
make_pair<"efgh",EFGHType>,
make_pair<"ijkl",IJKLType>
> stringToTypeMap;
Run Code Online (Sandbox Code Playgroud)
我使用了boost-fusion,但我找不到适合我的用例的正确解决方案,即字符串类型映射。
在我的工作场所,我们有一个不同的noreturn属性内部名称.假设它是INTERNAL_DONT_RETURN
我正在写一个类的成员函数,我在那里做类似的事情
INTERNAL_DONT_RETURN void foo() const
{
if(!*this)
{
throw CoolException();
}
m_call_throw();
}
Run Code Online (Sandbox Code Playgroud)
这个m_call_throw()是一个私有类成员std::function<void()>m_call_throw ,它作为lambda填充在类的构造函数中.这个lambda什么也没做
m_call_throw([uncoolID]() { throw UncoolException(uncoolID); })
现在两者,gcc-4.9.3和clang都给了我以下警告
error: function declared 'noreturn' should not return [-Werror,-Winvalid-noreturn]
}
^
1)编译器隐加入return作为解释在这里?
2)即使我抛出异常,为什么编译器认为我的函数会返回?
3)noreturn属性提到
当适用时,noreturn关键字不会影响异常路径:noreturn-marked函数仍然可以通过抛出异常或调用longjmp返回给调用者.
这与我的问题有关吗?
这是一个菜鸟问题.以下代码是否安全?
boost::unordered_set<std::string> func()
{
boost::shared_ptr<boost::unordered_set<std::string>> list =
boost::make_shared<boost::unordered_set<std::string>>();
/* Code to populate the shared_ptr to unordered_set goes here and I do
populate my set. */
return *list;
}
Run Code Online (Sandbox Code Playgroud)
首先会发生什么?shared_ptr通过导致内存故障复制/ NRVO /移动或破坏?如果不安全,我的替代方案是什么?
以下代码在VS 2015(更新3)和gcc 6.3(C++ 14)上编译正常,没有任何问题.
#include <string>
#include <locale>
int main()
{
std::u16string ustr = u"Android";
bool var = std::isspace(ustr[0],std::locale());
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但是,在clang/Xcode上它失败并出现以下错误
Error(s):
source_file.cpp:8:10: warning: unused variable 'var' [-Wunused-variable]
bool var = std::isspace(ustr[0],std::locale());
^
In file included from source_file.cpp:2:
In file included from /usr/include/c++/v1/locale:182:
/usr/include/c++/v1/__locale:705:44: error: implicit instantiation of undefined template 'std::__1::ctype<char16_t>'
return use_facet<ctype<_CharT> >(__loc).is(ctype_base::space, __c);
^
source_file.cpp:8:21: note: in instantiation of function template specialization 'std::__1::isspace<char16_t>' requested here
bool var = std::isspace(ustr[0],std::locale());
^
/usr/include/c++/v1/__locale:427:53: note: template is …Run Code Online (Sandbox Code Playgroud) 考虑一个独立的示例,其中我使用通配符查询目录中的所有名称:
#include <Windows.h>
#include <fstream>
void add_file(const std::string &path)
{
std::ofstream ofs(path,std::ofstream::out);
ofs.close();
}
void foo(const std::wstring& szDir)
{
std::cout << "f1 : FindFirstFileW\n";
WIN32_FIND_DATAW ffd;
HANDLE hFind = INVALID_HANDLE_VALUE;
hFind = FindFirstFileW(szDir.c_str(), &ffd);
if (INVALID_HANDLE_VALUE == hFind)
{
std::cout << "Error in FindFirstFileW : " << GetLastError() << std::endl;
return;
}
// List all the files in the directory with some info about them.
do
{
std::wcout <<"Long file name " << " " << ffd.cFileName << std::endl;
std::wcout …Run Code Online (Sandbox Code Playgroud) 可能吗?对于没有任何分支/循环的小代码。是否有gcc诸如SSE x86和其他处理器系列的标志或内在指令?我很好奇,因为这些天所有可用的处理器都遵循乱序执行模型。
提前致谢
这是我试图理解输出的代码.
class A
{
public:
A();
~A(){ cout<<"Destructor Called from A"<<endl;}
A(int x):Z(x){ cout<<"Constructor Called from A"<<endl; };
private:
int Z;
};
class B:public A
{
public:
B();
~B(){ cout<<"Destructor Called from B"<<endl;}
B(int x):A(x),Y(x){ cout<<"Constructor Called from B"<<endl; };
private:
int Y;
};
int main() {
A *a = new B(10);
delete a;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
为此,我得到了输出
Constructor Called from A
Constructor Called from B
Destructor Called from A
Run Code Online (Sandbox Code Playgroud)
我的问题是,类的析构函数发生了什么B?对象切片是否在这里起作用?
考虑下面一段代码,它基本上是迈耶单身人士的一个例子(希望如此)
static const std::string& foo() // Line 1
{
static std::string str("foo");
return str;
}
Run Code Online (Sandbox Code Playgroud)
第1行中提到的静态关键字是否无意义?如果是这样的话?
我正在尝试学习C++函数模板.我传递一个数组作为指向我的函数模板的指针.在那,我试图找到一个数组的大小.这是我使用的功能模板.
template<typename T>
T* average( T *arr)
{
T *ansPtr,ans,sum = 0.0;
size_t sz = sizeof(arr)/sizeof(arr[0]);
cout<<"\nSz is "<<sz<<endl;
for(int i = 0;i < sz; i++)
{
sum = sum + arr[i];
}
ans = (sum/sz);
ansPtr = &ans;
return ansPtr;
}
Run Code Online (Sandbox Code Playgroud)
即使我将指针传递给整数数组,该cout语句也会显示arras 的大小.现在我知道这可能是我之前提到的问题的可能重复,但我需要对此有更好的解释.15
我唯一能想到的是,因为模板是在运行时调用的,并且sizeof是一个编译时运算符,所以编译器只是忽略了这一行
int sz = sizeof(arr)/sizeof(arr[0]);
Run Code Online (Sandbox Code Playgroud)
因为在实际调用函数之前它不知道arr的确切类型.这是正确的还是我错过了这里的东西?将指针发送到函数模板也是可靠的吗?
c++ ×9
c++11 ×5
gcc ×2
visual-c++ ×2
boost ×1
boost-fusion ×1
boost-hana ×1
boost-mpl ×1
c++14 ×1
clang++ ×1
cpu ×1
lambda ×1
shared-ptr ×1
singleton ×1
sizeof ×1
static-cast ×1
templates ×1
winapi ×1
windows ×1