我正在创建类,这应该有助于解决C++的一些小缺陷,比如将double与其他double进行比较,以及定义的精度,等等。
我有名称空间Utilities,其中存在class Double:
namespace Utilities {
void init();
class Double {
public:
//! @brief Compares the equality of two doubles with defined precision
static bool fuzzyCompare(const double &d1,const double &d2)
此代码在GCC 11和Clang 13 ( C++20模式)中不加警告地编译。
struct A {
int x, y;
};
struct B : A { };
int main () {
A a{1,2};
B b{3,4}; // Clang 12 wants B b{{3,4}}
return a.x * b.x + a.y * b.y;
}
但在Clang 12我们得到
<source>:10:9: warning: suggest braces around initialization of subobject [-Wmis
N4687:
附件D(规范)兼容性特征
D.1静态数据成员的读取
1为了与先前的C++国际标准兼容,可以在没有初始化器的情况下冗余地在类之外重新声明一个常设静态数据成员。这种用法是不可取的。[例子:
struct A {
static constexpr int n = 5; // definition (declaration in C++ 2014)
};
constexpr int A::n; // redundant declaration (definition in C++ 2014)
-最终例子]
我英语不好,所以我在学习标准时遇到了几个问题(我只是来自odr-使用)
当我有一个具有static const或constexpr数据成员的类时,定义该变量使我不得不重复如下内容:
/// my_class.hpp
class my_class { constexpr static int x = 1; };
/// my_class.cpp
#include "my_class.hpp"
// auto my_class::x; // error: declaration of 'auto my_class::x' has no initializer
// decltype(auto) my_class::x; // error
我在<experimental/filesystem> include中发现了一个简洁的方法,它可以告诉我路径是文件还是目录,但在g++ v7.4.0上使用它时,它给了我一个错误。我尝试使用的方法是:.is_regular_file()。 它甚至在cppref上,https://en.cppreference.com/w/cpp/experimental/fs/is。 但是在编译时,我得到了"...has no member named 'is_regular_file'“错误。我不能使用常规文件系统,因为我的目标系统(Ubuntu18.04LTS)已经预
是否有一种方法使用初始化程序列表来初始化条形图?
struct Foo {
int i[2];
};
struct Bar : Foo {};
Foo f{0, 1}; // OK
Bar b{0, 1}; // error: no matching function for call to
// ‘Bar::Bar(< brace-enclosed initializer list>)’
背景
我们试图“借用”一些使用内联静态变量的代码。该代码不带任何警告,符合C++17编译器。但是,对于较低版本的编译器,它会发出以下警告:
warning: inline variables are only available with ‘-std=c++17’ or ‘-std=gnu++17’
我们已经能够通过命令行开关来抑制衰退,但是我想知道是否有一种方法可以重构代码来抑制警告。
我已经能够用下面的示例代码重现这个问题:
示例代码
#include <iostream>
using namespace std;
template <typename T>
c
我有一个包含另一个类模板的类模板,并且内部模板具有显式的专门化:
template <typename Outer>
struct ContainingClass {
template <typename T>
struct Rule {
Rule(T value);
// ... other members ...
};
template <>
struct Rule<void> {
Rule();
// ... different members than the non-void R
用-std=c++14编译以下代码:
#include <memory>
class A
{
public:
static constexpr int c = 0;
std::shared_ptr<int> b;
A() {
b = std::make_shared<int> (c);
}
};
int main () {
A a;
return 0;
}
给出链接器错误“对‘A::C’的未定义引用”,当在其他非"make_shared“上下文中使用"A::c”时
我有一些处理函数地址、lambda和模板参数的代码。
下面是一个代码片段,它向我展示了这些函数地址的一些用法:
#include <type_traits>
int main() {
auto l = []{};
using L = decltype(l);
// fine, am I just lucky?
using type1 = std::integral_constant<decltype(&L::operator()), &L::operator()>;
struct Bar {
我有这个代码片段,它是用VS2013编译的,但是在VS2017中给出了C2059错误。
template <typename ArgumentType>
class MyFunction: public std::unary_function<ArgumentType, ArgumentType>
{
// rest of the code here
}
错误C2143: Syntaxfehler: Es fehlt ",vor“<
我查阅了错误的文档,但无法找出错误的原因。
我试过了
template <typename ArgumentT
#include <iostream>
using namespace std;
class test
{
public:
register int a;
};
int main() {
// your code goes here
test t;
t.a = 10;
return 0;
}
我收到以下错误:
错误:为“a”指定的存储类
是否有任何方法使用寄存器存储类作为成员变量?
当我试图用特定的编译器版本编译时,我得到了一个编译器错误。即。ICC17.0与-std=c++17 -O3
编译器错误:
source>(19): error: no suitable user-defined conversion from "Data" to "std::__cxx11::string" exists
Data temp{std::forward<Data>(d)};
^
compilation aborted for <source> (code 2)
ASM ge
比方说,出于我自己的原因,我希望类有一个非静态引用成员。我认为这种类型应该很容易地从大多数使用它的代码中优化。因此,我在单元测试中断言它的琐碎性。
Clang和GCC同意这门课是微不足道的,但MSVC不同意。根据标准,谁是对的,为什么?
#include <type_traits>
struct example {
int& r;
};
// Clang and GCC let this pass
// MSVC fires this assertion
static_assert(
std::is_trivial<example>::va