我有一个自定义容器类和定义的迭代器,所以我可以这样做:
for (auto i : c)
Run Code Online (Sandbox Code Playgroud)
但有反向迭代的东西吗?
就像是:
for_reverse (auto i : c)
Run Code Online (Sandbox Code Playgroud) 使用以下代码我得到警告:
warning: specialization of ‘template<class _Iterator> struct std::iterator_traits’ in different namespace [-fpermissive]
template<> class std::iterator_traits<Token_ptr>{
public:
typedef Word difference_type;
typedef Word value_type;
typedef Token_ptr pointer;
typedef Word& reference ;
typedef std::bidirectional_iterator_tag iterator_category ;
};
Run Code Online (Sandbox Code Playgroud)
虽然一切正常,但是任何人都知道究竟是什么意思,为什么会发出警告.(g ++发出警告而clang ++没有).
据我所知,我可以将一个字面值分配给字符串:
std::string s="good";
std::wstring s=L"good";
Run Code Online (Sandbox Code Playgroud)
我该如何分配给
std::u16string s=
std::u32string s=
Run Code Online (Sandbox Code Playgroud) 是否有一种标准方法来比较两个身份引用,基本上实现了以下操作:
bool compareForIdentity(int& a,int& b){return &a==&b;}
Run Code Online (Sandbox Code Playgroud) 我正在学习如何重载" - >"并且文档说:"operator->在它返回的值上再次被调用,直到到达operator->返回一个普通指针.之后,内置语义被应用于那个指针."
虽然很清楚文档说的是什么,但实质上是一个类的重载" - >"可以使用一个"特殊指针",它本身有一个重载的" - >",它可以给出一个"特殊指针"等等直到"平原"指针"找到了,我找不到真正使用它的例子(除非它用于查找链表最后一个元素).
有人可以解释一下幕后的内容是什么(因为这种可能性没有提供"普通指针" - 所以我没有看到任何理由为它提供"特殊指针").
现实世界使用的一个例子也可能有所帮助,因为我可能错过了应用该行为的模型.
另一方面可能需要避免这种行为,怎么可能呢?
名为文件"main000.cpp"的代码在linux机器上使用后续命令行编译和执行:
g++ -std=c++11 -m32 -O0 main000.cpp ; ./a.out
Run Code Online (Sandbox Code Playgroud)
并正常执行
而如果编译并执行将O0改为O1
g++ -std=c++11 -m32 -O1 main000.cpp ; ./a.out
Run Code Online (Sandbox Code Playgroud)
不起作用并生成堆栈转储
谁知道为什么?
#include <stdio.h>
#include <string>
using namespace std;
typedef unsigned int Token;
const Token tokenBase=0x80000000;
enum classId{
classNoId=tokenBase,
classPrimitiveId,
};
struct classInfo{classId id;wstring name;};
classInfo classInfos[]={
{classNoId,L"classNoID"},
{classPrimitiveId,L"classPrimitiveId"},
};
int main(){
int id=classInfos[0].id;
return 0*id;
}
Run Code Online (Sandbox Code Playgroud) 我想编写代码如下:
template<typename T> const int a;
template<> const int a<float>=5;
template<> const int a<double>=14;
template<> const int a<char>=6;
template<> const int a<wchar>=33;
Run Code Online (Sandbox Code Playgroud) 我想初始化一些类成员,并且"expected parameter declarator"在使用g ++时我得到错误
(使用clang ++)"expected identifier before numeric constant".
所以我再次读取类初始化并编写下面的代码:
#include <stdio.h>
class AAA{
public:
int l;
AAA(int i){l=i;}
};
class BBB{
bool normal;
AAA aaa=10;
AAA bbb(20);
AAA ccc{30};
AAA ddd={45};
};
int main(int argc, char **argv){printf("hello world\n");return 0;}
Run Code Online (Sandbox Code Playgroud)
看来语法AAA bbb(20)不被接受!
这是正常的吗?(我使用选项-std = c ++ 11).
或者我错过了一些观点?
使用gcc,我需要跟踪在各种例程中分配的一些特殊变量的存在作为局部变量.为了做到这一点,我想在编译时发出信号.所以我需要在编译时获得一个方法来获取相对于堆栈帧的自动变量(具有本地范围的变量)的偏移量(相对于x86体系结构中的rbp寄存器).
我已经能够得到如下:
(char*)&a - (char*)__builtin_frame_address(0)
Run Code Online (Sandbox Code Playgroud)
问题是gcc不够智能,并且只能为常量的加载生成大量指令.所以我想知道是否偶然有任何隐藏的宏或内置直接给我,因为这是更简单的解决方案.
我需要一个int类型(或类似的)并附加到它的方法.
所以我正在使用:
struct INT{
int i;
operator int(){return i;};
int operator=(int v){return i=v;}
void p(){std::cout<<"i="<<i<<std::endl;}
};
int main(int argc, char **argv)
{
INT X;
int i=8;
X=5;
int j=X+i;
X=j;
(*reinterpret_cast<INT*>(&i)).p();
reinterpret_cast<INT>(i).p();
X.p();
}
Run Code Online (Sandbox Code Playgroud)
虽然第二种形式的reinterpret_cast应该有效,但它没有明显的语义解释.你知道为什么吗 ?
我用g ++编译了以下代码并且它有效.
bool keyExists(Obj key){
findIn(key,true,false,false,nullptr,nullptr,1,0,0);
}
Run Code Online (Sandbox Code Playgroud)
我使用clang ++编译它,当程序运行时它冻结了.
我把线改为:
bool keyExists(Obj key){
return findIn(key,true,false,false,nullptr,nullptr,1,0,0);
//findIn(key,true,false,false,nullptr,nullptr,1,0,0);
}
Run Code Online (Sandbox Code Playgroud)
现在它的工作原理.
我想它不应该那样工作.这是Clang的已知错误还是一个特例?
我尝试了以下
auto ns=ms.capture[0].init,nl=ms.capture[0].len;
Run Code Online (Sandbox Code Playgroud)
因为我懒得查找init和len字段的类型。编译器当然不喜欢它。
我的理解是:
ttt xx,yy;
Run Code Online (Sandbox Code Playgroud)
相当于
ttt xx;ttt yy;
Run Code Online (Sandbox Code Playgroud)
那么应该同样的工作auto吗?也就是说,不应该
auto xx, yy;
Run Code Online (Sandbox Code Playgroud)
变得:
auto xx, auto yy;
Run Code Online (Sandbox Code Playgroud)
或者有什么我不明白的细节?还是我的编译器有问题?