如果在声明中初始化了非静态数据成员,它们是否允许将非静态数据成员声明为"auto"?例如:
struct S
{
auto x = 5; // in place of 'int x = 5;', which is definitely allowed
};
Run Code Online (Sandbox Code Playgroud)
GCC 4.7拒绝上述代码,但它接受了int x = 5;.
假设这不是编译器错误,而是标准真的不允许它,为什么不呢?它与声明局部变量一样有用auto.
该铛 C++编译器声称通过提供对IDE使用这样的任务,解析代码的API来构建的,除其他事项外,更好的IDE集成.
那么,是否有任何优秀的C++ IDE使用clang来提供诸如语义突出显示,重构以及实时查找和显示语义错误等功能?
我一直在使用Eclipse CDT,但它的C++解析器充满了缺陷,导致IDE在代码中报告了许多恼人的误报错误.我希望有一个IDE报告错误,当且仅当编译器报告相同的错误时,因此我对构建在编译器内部的IDE的兴趣.
我主要对跨平台IDE感兴趣,虽然我不介意了解Windows或Linux的单平台IDE(因此不是 Xcode),只要它们是FOSS(不是 Xcode的另一个原因).
我们知道C++模板元编程是Turing完整的,但预处理器元编程却不是.
C++ 11为我们提供了一种新形式的元编程:constexpr函数的计算.这种计算形式是图灵完备吗?我在想,因为在constexpr函数中允许递归和条件运算符(?:),它会是,但我希望有更多专业知识的人来确认.
I have discovered a disturbing inconsistency between std::string and string literals in C++0x:
#include <iostream>
#include <string>
int main()
{
int i = 0;
for (auto e : "hello")
++i;
std::cout << "Number of elements: " << i << '\n';
i = 0;
for (auto e : std::string("hello"))
++i;
std::cout << "Number of elements: " << i << '\n';
return 0;
}
Run Code Online (Sandbox Code Playgroud)
The output is:
Number of elements: 6
Number of elements: 5
Run Code Online (Sandbox Code Playgroud)
I understand the mechanics of why this …
有没有办法在原始文件描述符而不是FILE*上执行ftell()所做的操作(返回文件中的当前位置)?我认为应该有,因为你可以使用lseek()在原始文件描述符上寻找.
我知道我可以使用fdopen()创建一个对应于文件描述符的FILE*,但我宁愿不这样做.
也就是说,为什么这样:
struct S {};
struct T
{
T(S& s) : s{s} {}
S& s;
};
int main()
{
S s;
T t{s};
}
Run Code Online (Sandbox Code Playgroud)
给我一个GCC 4.7的编译器错误:
test.cpp: In constructor 'T::T(S&)':
test.cpp:5:18: error: invalid initialization of non-const reference of type 'S&' from an rvalue of type '<brace-enclosed initializer list>'
Run Code Online (Sandbox Code Playgroud)
?
要修复错误,我必须更改s{s}为s(s).这不会打破统一初始化的统一性吗?
编辑:我试过clang,clang接受它,所以也许这是一个GCC错误?
有没有办法在C++中编写一个接受左值和右值参数的函数,而不是模板?
例如,假设我编写了一个print_stream从a 读取istream并打印读取到屏幕的数据的函数.
我认为print_stream像这样打电话是合理的:
fstream file{"filename"};
print_stream(file);
Run Code Online (Sandbox Code Playgroud)
以及像这样:
print_stream(fstream{"filename"});
Run Code Online (Sandbox Code Playgroud)
但是我如何声明print_stream这两种用途都有效呢?
如果我声明它
void print_stream(istream& is);
Run Code Online (Sandbox Code Playgroud)
然后第二次使用将无法编译,因为rvalue不会绑定到非const左值引用.
如果我声明它
void print_stream(istream&& is);
Run Code Online (Sandbox Code Playgroud)
然后第一次使用将无法编译,因为左值不会绑定到右值引用.
如果我声明它
void print_stream(const istream& is);
Run Code Online (Sandbox Code Playgroud)
那么函数的实现将无法编译,因为你无法读取const istream.
我不能使该函数成为模板并使用"通用引用",因为它的实现需要单独编译.
我可以提供两个重载:
void print_stream(istream& is);
void print_stream(istream&& is);
Run Code Online (Sandbox Code Playgroud)
并且第二次调用第一次,但这似乎是很多不必要的样板,并且我发现每次用这样的语义编写函数时都必须这样做非常不幸.
我能做些什么吗?
对于以下代码:
auto F(int count) -> decltype([](int m) { return 0; })
{
return [](int m) { return 0; };
}
Run Code Online (Sandbox Code Playgroud)
g ++ 4.5给出错误:
test1.cpp:1:32: error: expected primary-expression before 'int'
test1.cpp:1:32: error: expected ')' before 'int'
Run Code Online (Sandbox Code Playgroud)
问题是什么?从函数返回lambda的正确方法是什么?
您可以使用初始化列表构建一个std :: array:
std::array<int, 3> a = {1, 2, 3}; // works fine
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试从std::initializer_list作为类中的数据成员或基础对象构造它时,它不起作用:
#include <array>
#include <initializer_list>
template <typename T, std::size_t size, typename EnumT>
struct enum_addressable_array : public std::array<T, size>
{
typedef std::array<T, size> base_t;
typedef typename base_t::reference reference;
typedef typename base_t::const_reference const_reference;
typedef typename base_t::size_type size_type;
enum_addressable_array(std::initializer_list<T> il) : base_t{il} {}
reference operator[](EnumT n)
{
return base_t::operator[](static_cast<size_type>(n));
}
const_reference operator[](EnumT n) const
{
return base_t::operator[](static_cast<size_type>(n));
}
};
enum class E {a, b, c};
enum_addressable_array<char, …Run Code Online (Sandbox Code Playgroud) 我试图声明一个constexpr指针初始化为一些常量整数值,但clang正在挫败我所有的尝试:
尝试1:
constexpr int* x = reinterpret_cast<int*>(0xFF);
test.cpp:1:20: note: reinterpret_cast is not allowed in a constant expression
Run Code Online (Sandbox Code Playgroud)
尝试2:
constexpr int* x = (int*)0xFF;
test.cpp:1:20: note: cast which performs the conversions of a reinterpret_cast is not allowed in a constant expression
Run Code Online (Sandbox Code Playgroud)
尝试3:
constexpr int* x = (int*)0 + 0xFF;
test.cpp:1:28: note: cannot perform pointer arithmetic on null pointer
Run Code Online (Sandbox Code Playgroud)
是我试图不允许的设计?如果是这样,为什么?如果没有,我该怎么办?
注意:gcc接受所有这些.