考虑以下代码:
#include <vector>
#define BROKEN
class Var {
public:
#ifdef BROKEN
template <typename T>
Var(T x) : value(x) {}
#else
Var(int x) : value(x) {}
#endif
int value;
};
class Class {
public:
Class(std::vector<Var> arg) : v{arg} {}
std::vector<Var> v;
};
Run Code Online (Sandbox Code Playgroud)
不管是否BROKEN定义,Clang ++(7.0.1)都会编译此错误,而如果BROKEN已定义,则g ++(8.2.1)会引发错误:
main.cpp:9:20: error: cannot convert ‘std::vector<Var>’ to ‘int’ in initialization
Var(T x) : value(x) {}
^
Run Code Online (Sandbox Code Playgroud)
据我所知,std::vector(std::vector&&)在两种情况下,这里使用的统一初始化都应选择构造函数。但是,显然,g++它将{arg}视为初始化列表,并尝试初始化应用于向量的vwith 的第一个元素Var,这将不起作用。
如果BROKEN未定义,则g ++显然足够聪明,可以意识到initializer_list重载将无法正常工作。
哪个是正确的行为,或者标准都允许? …
使用C++类,您可以让派生类从其父类继承变量.如何定义派生类,以便var2不继承derivclass?
class mainclass{
public:
int var1;
char var2;
void test(){
cout<<var1<<var2<<endl;
}
}
class derivclass : mainclass{
public:
void test(){
cout<<var1<<var2<<endl;
//want a compiler error here that var2 is not defined
}
}
Run Code Online (Sandbox Code Playgroud) 我正在开发一个OSX应用程序,在其中,我想知道当前目录是什么(即保存.app-file的目录).
目前,我正在使用以下代码:
NSString *dir=[[NSFileManager defaultManager] currentDirectoryPath];
[[NSAlert alertWithMessageText:@"dir"
defaultButton:@"OK"
alternateButton:nil
otherButton:nil
informativeTextWithFormat:dir] runModal];
Run Code Online (Sandbox Code Playgroud)
当从Xcode(运行按钮)运行时,这给了我调试目录(这是我正在寻找的),但是当在Finder中双击应用程序时(因此,在调试目录中),它给了我/哪些谜题我.
为什么会发生这种情况?如何可靠地获取当前目录?
我正在尝试检测标准输入上是否有数据可供我读取。
具体来说,我已经使用 关闭了规范模式tcsetattr,因此我可以一次读取一个字符(阻塞)。我想检测转义序列,例如由箭头键产生的转义序列,但将它们与单独的转义键区分开来。此外,我想快速知道输入了哪些内容;我假设我的终端相当快,并且后面的转义序列的其余部分也^[相当快。
假设我已经知道(例如使用select(2))标准输入上有一些东西可以读。我使用 读取一个字符getchar(),它是^[, ASCII 27。现在我想知道在某个时间间隔内是否有更多字符出现,或者这个转义字符就是一切(这将表明按下了转义键)。用于select(2)此似乎不起作用,因为(我注意到,并在其他地方读到)其余字符已经被缓冲,所以select(2)没有什么可检测的了。所以我转向ioctl(2)使用FIONREAD,但这似乎也不起作用。
最小(非)工作示例:
#include <stdio.h>
#include <stdlib.h>
#include <termios.h>
#include <sys/select.h>
#include <sys/ioctl.h>
#include <assert.h>
struct termios tios_bak;
void initkeyboard(void){
struct termios tios;
tcgetattr(0,&tios_bak);
tios=tios_bak;
tios.c_lflag&=~ICANON;
tios.c_cc[VMIN]=1; // Read one char at a time
tios.c_cc[VTIME]=0; // No timeout on reading, make it a blocking read
tcsetattr(0,TCSAFLUSH,&tios);
}
void endkeyboard(void){
tcsetattr(0,TCSAFLUSH,&tios_bak);
}
int main(void){
initkeyboard();
atexit(endkeyboard);
printf("Press an …Run Code Online (Sandbox Code Playgroud) 我正在尝试为iPhone(或iPad)构建一个应用程序,我想在其中运行一些shell命令.我希望它工作的iPhone 不会越狱.
该system()命令似乎在执行shell命令时有效,但输出和输入当然仍然是个问题.我了解到NSTask,虽然没有记录,但它可以用于这些类型的东西并且它存在.(包括Mac NSTask.h似乎工作得很好)
现在的问题是,在执行此代码时:
NSTask *task;
task=[[NSTask alloc] init];
task.launchPath=@"/usr/bin/ls";
task.arguments=[NSArray array];
NSPipe *pipe=[NSPipe pipe];
task.standardOutput=pipe;
task.standardError=pipe;
NSFileHandle *output=pipe.fileHandleForReading;
pipe=[NSPipe pipe];
task.standardInput=pipe;
[task launch];
Run Code Online (Sandbox Code Playgroud)
pipe未来的I/O 在哪里,它会产生一个错误读数:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'launch path not accessible'
Run Code Online (Sandbox Code Playgroud)
我已经为unix bin目录尝试了很多可能的路径,但要么我找不到它,要么我的应用程序没有列出文件的权限.-.-
如何在控制该命令的输入和输出的同时在iPhone上执行shell命令?