在 Visual Studio 代码中,它具有Copy Path
和Copy Relative Path
(Ctrl+Shift+p->File: Copy Path of Active File) 的命令。有没有办法可以复制活动文件名而不是整个路径?
如果没有办法复制活动文件名。有没有办法可以编写脚本或基于现有命令创建新命令Copy Path
并在Command Palette
.
在C++ 17中,fold表达式可用,因此要打印参数,我们可以使用
#define EOL '\n'
template<typename ...Args>
void output_argus(Args&&... args)
{
(cout << ... << args) << EOL;
}
int main()
{
output_argus(1, "test", 5.6f);
}
Run Code Online (Sandbox Code Playgroud)
有输出
1test5.6
如果我想使用fold表达式'\n'
为每个元素添加额外的字符以获得以下结果,该怎么办?
1
test
5.6
Run Code Online (Sandbox Code Playgroud)
这甚至可能吗?如果有,怎么样?
这是一个获取数组大小的宏
#define array_size(array) \
(sizeof( array ) / (sizeof( array[0] ) * (sizeof( array ) != sizeof(void*) || sizeof( array[0] ) <= sizeof(void*)))
Run Code Online (Sandbox Code Playgroud)
我认为通常(sizeof(array)/(sizeof(array [0]))足以获得数组的大小.
我想这部分
(sizeof( array[0] ) * (sizeof( array ) != sizeof(void*) || sizeof( array[0] ) <= sizeof(void*))
Run Code Online (Sandbox Code Playgroud)
是为了避免将整个事物除以零,任何人都可以帮忙解释一下?
提前致谢.
干杯,
这是一个c ++函数:
int FuncWithoutReturn()
{
int var = 10;
++var;
// No return value here !!!
}
Run Code Online (Sandbox Code Playgroud)
在MSVC中,编译器生成错误:
错误C4716:'FuncWithoutReturn':必须返回一个值.
但在XCode 5中,编译器只是发出警告:
控制到达非空函数的结束
在运行时,如果我很幸运,应用程序崩溃.我知道这是一个愚蠢的错误,但编译器首先产生错误会很好.
只是想知道为什么XCode认为这是一个警告而不是错误.
我有以下代码仅用于测试,模板函数使用两次,第一次没问题.在第二种情况下,我创建一个指针并将指针作为参考传递给模板函数.我期望行"obj.~T()"编译时失败.但实际上代码编译并运行良好.当我通过函数调试时,程序只是跳过obj.~T(); 我正在使用VC10.
我的问题是:
1.这是预期的行为吗?如果它基于c ++标准的哪个部分?
2.关于这种行为的任何赞成和反对?在我的情况下它是好的,因为没有编译错误既没有运行时错误.但可能有一些情况我不知道,但确实损坏了代码.
谢谢
template<typename T>
void ptrDest(T& obj)
{
obj.~T();
}
class Dummy
{
public:
Dummy(){}
~Dummy(){ cout << "dest" <<endl;}
};
int main()
{
Dummy d;
ptrDest(d);
Dummy* pd = new Dummy();
ptrDest(pd);
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我正在使用eclipse和cygwin.该应用程序是64位.在cygwin中,结构定义为:
struct addrinfo {
int ai_flags; /* input flags */
int ai_family; /* address family of socket */
int ai_socktype; /* socket type */
int ai_protocol; /* ai_protocol */
socklen_t ai_addrlen; /* length of socket address */
char *ai_canonname; /* canonical name of service location */
struct sockaddr *ai_addr; /* socket address of socket */
struct addrinfo *ai_next; /* pointer to next in list */
};
Run Code Online (Sandbox Code Playgroud)
sizeof(addrinfo)结果为48. socketlen_t的大小为4个字节.int类型大小为4个字节.指针在64位应用程序中是8个字节.总字节数为44(4个字节= 16个字节,socket_len = 4个字节,3个指针= 24; 20 + 4 + 24 = 44).我想知道丢失的4个字节是什么?它们是用于填充吗?我认为44字节不需要对齐.任何想法?
谢谢你提前回答.
我正在检查setg()
班级中的功能std::basic_streambuf
。它在文件 streambuf 中定义。
gcc 版本是 5.4.0
我使用的编译选项是 -std=gnu++14
void
setg(char_type* __gbeg, char_type* __gnext, char_type* __gend)
{
_M_in_beg = __gbeg;
_M_in_cur = __gnext;
_M_in_end = __gend;
}
Run Code Online (Sandbox Code Playgroud)
成员变量声明为:
char_type* _M_in_beg; ///< Start of get area.
char_type* _M_in_cur; ///< Current read area.
char_type* _M_in_end; ///< End of get area.
char_type* _M_out_beg; ///< Start of put area.
char_type* _M_out_cur; ///< Current put area.
char_type* _M_out_end; ///< End of put area.
Run Code Online (Sandbox Code Playgroud)
我知道由于成员变量是非常量的char_type*
,所以函数setg()
只能采用非常量参数。但是这些指针的目的是检索东西,这些指针char_type *
应该首先声明为const …
要创建一个新的 polymophic_allocator,ppl 可以使用一些默认资源
std::pmr::polymorphic_allocator<char> newdel { std::pmr::new_delete_resource() };
std::pmr::polymorphic_allocator<char> synced { std::pmr::synchronized_pool_resource() };
std::pmr::polymorphic_allocator<char> nonsync { std::pmr::unsynchronized_pool_resource() };
Run Code Online (Sandbox Code Playgroud)
我的问题是new_delete_resource
线程安全的?
我正在使用GLFW来处理应用程序中的窗口事件.它工作正常.后来我决定从GLFWwindow开始删除原始指针.它在文件glfw3.h中定义为:
typedef struct GLFWwindow GLFWwindow;
Run Code Online (Sandbox Code Playgroud)
而且我在头文件中找不到结构的实际定义.所以我认为这是一种前瞻性声明,但我不知道为什么它不喜欢
struct GLWwindow;
Run Code Online (Sandbox Code Playgroud)
我试图使用后面的前向声明形式来替换前一种形式.它汇编很好.前者形成前沿宣言的亲是什么?
所以真正的问题,由于GLFWwindow结构只是一个声明,唯一指针无法完成模板专业化而无需定义.我不能使用unique_ptr来声明任何指针.编译器给我错误
C2027 use of undefined type 'GLFWwindow'
C2338 can't delete an incomplete type
C4150 deletion of pointer to incomplete type 'GLFWwindow';no destructor called
Run Code Online (Sandbox Code Playgroud)
任何人都知道如何使用GLFWwindow的唯一指针?
谢谢
在main
函数中,我创建了一个const int
指针变量,将其赋值给一个声明的变量auto&
.然后使用decltype(x)
来检查类型.我预计类型是const int*
.但is_same
回报false
.
int main()
{
int a = 10;
const int * cp_val= &a;
auto& x = cp_val;
bool is_const_int_ptr = std::is_same<decltype(x), const int *>::value; // returns 0
// *x = 100; // error: assignment of read-only location '* x'
}
Run Code Online (Sandbox Code Playgroud)
但是如果我添加以下辅助函数:
#include <boost/type_index.hpp>
template<typename T>
void print_type(T)
{cout << "type T is: "<< boost::typeindex::type_id_with_cvr<T>().pretty_name()<< '\n';}
Run Code Online (Sandbox Code Playgroud)
在主要的,我调用函数
print_type(x); // It returns int const*
Run Code Online (Sandbox Code Playgroud)
我错过了什么std::is_same …