C++标准化委员会中有一个研究小组,在C++ 1z或之后提供编译时反射.我想知道预期工具的目的是什么以及功能有多强大?
例如,是否可以使用这些工具命名函数或类?
struct A {int f() {return 42;}};
struct B {int (std::reflect<A>::member<0>::declname)() {return 43;}};
// equivalent to struct B {int f() {return 43;}};
Run Code Online (Sandbox Code Playgroud)
如果它没有这个那么强大,那么典型的用例是什么?
我一直在阅读有关C++模块提案(最新草案)的内容,但我并不完全了解它旨在解决的问题.
它的目的是允许由一个编译器构建的模块被任何其他编译器使用(当然,在相同的OS /架构上)?也就是说,提案是否相当于标准化C++ ABI?
如果没有,是否有另一个提议被认为是标准化C++ ABI并允许编译器互操作?
我正在阅读的STL代码可能很旧......但问题与C++模板语法有关.
问题围绕着这个stl模板函数:
template<class T> std::destroy(T *p) {
p->~T();
}
Run Code Online (Sandbox Code Playgroud)
我似乎无法找到std :: destroy(T*)函数的特化.所以在我看来,模板函数将为"int"类型实例化相同,并调用"int"的析构函数.为了说明我的观点,我创建了这个模拟std :: destroy的示例代码.我称之为my_destroy,就是这个例子.
#include <iostream>
#include <stdio.h>
using namespace std;
template <class T>
void my_destroy(T * pointer) {
pointer->~T();
}
int main()
{
int *a;
//a->~int(); // !!! This won't compile.
my_destroy<int>(a); // !!! This compiles and runs.
}
Run Code Online (Sandbox Code Playgroud)
}
令我惊讶的是,这行不编译:
a->~int();
Run Code Online (Sandbox Code Playgroud)
但这一行编译:
my_destroy<int>(a);
Run Code Online (Sandbox Code Playgroud)
我的困惑是,我认为这my_destroy<int>(a)将被实例化为相当于a->~int();
对于更大的上下文中的问题,当STL容器<int>擦除元素时,如何std::destroy()工作?
我被以下代码严重咬了,我浪费了很多宝贵的时间.
#include<string>
int next(std::string param){
return 0;
}
void foo(){
next(std::string{ "abc" });
}
Run Code Online (Sandbox Code Playgroud)
这会产生以下编译器错误(在Visual Studio 2013上):
1>------ Build started: Project: sandbox, Configuration: Debug Win32 ------
1> test.cpp
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility(371): error C2039: 'iterator_category' : is not a member of 'std::basic_string<char,std::char_traits<char>,std::allocator<char>>'
1> c:\users\ray\dropbox\programming\c++\sandbox\test.cpp(8) : see reference to class template instantiation 'std::iterator_traits<std::basic_string<char,std::char_traits<char>,std::allocator<char>>>' being compiled
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility(371): error C2146: syntax error : missing ';' before identifier 'iterator_category'
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility(371): error C4430: …Run Code Online (Sandbox Code Playgroud) 在C++中同时使用virtual和override使用函数是危险的吗?那是否会让你因重载而产生歧义?
显然,virtual必须在基类中使用,这将是愚蠢的,不使用override派生类,但它实际上是有问题的使用virtual 与 override在派生类?
试图确定这是一个风格或正确的问题.
例:
class Widget {
virtual void transmogrify() = 0;
}
class Gadget : public Widget {
virtual void transmogrify() override {}
}
Run Code Online (Sandbox Code Playgroud) 自学C并发现当我为温度转换进行等式时,除非我将分数更改为小数,否则它将无效.即
tempC=(.555*(tempF-32)) 会工作,但tempC=((5/9)*(tempF-32)) 不会奏效.
为什么?
根据C Primer Plus,它应该可以工作,因为我正在使用tempC和tempF的浮子.
(可能不是C++ 14,可能是库TS)工具make_optional被定义为(在n3672中):
template <class T>
constexpr optional<typename decay<T>::type> make_optional(T&& v) {
return optional<typename decay<T>::type>(std::forward<T>(v));
}
Run Code Online (Sandbox Code Playgroud)
为什么有必要改变类型T(即不仅仅是返回optional<T>),是否有一种哲学(以及实际)理由用于decay转换?
用于查询程序运行的硬件或操作系统功能属性的标准C++功能和实用程序是什么?
例如,std::thread::hardware_concurrency()为您提供机器支持的线程数.
但是,如何检测计算机具有多少RAM,或者进程使用了多少RAM,或者在某个目录中可以写入多少磁盘空间,或者有多少L2缓存可用?
假设以下代码:
namespace test
{
namespace detail
{
}
inline namespace v1
{
namespace detail
{
void foo()
{
}
}
}
}
int main()
{
test::detail::foo();
}
Run Code Online (Sandbox Code Playgroud)
我们可以看到,这段代码与Clang编译; 然而,与海湾合作委员会没有关系 - 海湾合作委员会抱怨说这namespace detail是不明确的:
main.cpp:20:11: error: reference to 'detail' is ambiguous
test::detail::foo();
^
main.cpp:4:5: note: candidates are: namespace test::detail { }
{
^
main.cpp:10:9: note: namespace test::v1::detail { }
{
^
Run Code Online (Sandbox Code Playgroud)
哪个编译器在这里做正确的事情?
在Ada中,可以使用下划线来编写用于分隔数字的数字,这极大地提高了可读性.例如:1_000_000(相当于1000000)C++有类似的方法吗?
编辑:这是关于源代码的问题,而不是I/O.