首选是这样的:
template<typename T>
bool isNotZero(const T &a)
{
if (std::is_floating_point<T>::value) return abs(a) > std::numeric_limits<T>::epsilon();
else return a;
}
Run Code Online (Sandbox Code Playgroud)
或这个:?
template<typename T>
std::enable_if<std::is_floating_point<T>::value, bool>::type
isNotZero(const T &a) { return abs(a) > std::numeric_limits<T>::epsilon(); }
template<typename T>
std::enable_if<std::is_integral<T>::value, bool>::type
isNotZero(const T &a) { return a; }
Run Code Online (Sandbox Code Playgroud)
我通常使用第一种类型来避免许多版本的功能.
我相信它完全一样.
第一个版本在操作码阶段优化,第二个版本在模板实例化阶段优化.
std::vector<int> v1(1000);
std::vector<std::vector<int>> v2(1000);
std::vector<std::vector<int>::const_iterator> v3(1000);
Run Code Online (Sandbox Code Playgroud)
如何初始化这3个向量的元素?
关于int,我测试它,我看到所有元素都变成0.这是标准吗?我相信原语仍未定义.我创建一个包含300000000个元素的向量,给出非零值,删除它并重新创建它,以避免操作系统内存清除数据安全.重建矢量的元素也是0.
迭代器怎么样?默认构造函数是初始值(0)还是初始值仍未定义?当我检查这个时,迭代器指向0,但这可能是操作系统
当我创建一个特殊的对象来跟踪构造函数时,我看到对于第一个对象,vector运行默认构造函数,而对于所有其他对象,它运行复制构造函数.这是标准吗?
有没有办法完全避免元素的初始化?或者我必须创建自己的矢量?(哦,我的上帝,我总是说不是另一个矢量实现)我问,因为我使用超大的稀疏矩阵并行处理,所以我不能使用push_back(),当然我不想无用的初始化,后来我会改变值.
为什么第一个调用无法编译?
auto get1 = []<int B>() { return B; };
auto get2 = []<typename B>(B b) { return b; };
int main()
{
get1<5>(); // error: no match for operator<
get2(5); // ok
}
Run Code Online (Sandbox Code Playgroud)
我之所以使用它,是因为该表达式在代码中多次重复。
当然,我可以使用真实的函数模板,但是我很好奇。
以下代码将构造函数从base转发为派生类.
为什么2个复制构造函数调用?在后台发生了什么?
用g ++编译.
#include <iostream>
using namespace std;
struct A {
A() { cout << "A" << endl; }
A(const A&) { cout << "A(const A&)" << endl; }
template<typename T> A(T a); // Needed to compile :-O
};
template<typename T>
struct C : public T { using T::T; };
int main()
{
A a;
C<A> ca(a);
//C<A> caa(ca);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出是:
A
A(const A&)
A(const A&)
Run Code Online (Sandbox Code Playgroud) 我得到了以下消息的大量输出(仅由十六进制地址区分):
ld.exe: DWARF error: could not find variable specification at offset 101cee
Run Code Online (Sandbox Code Playgroud)
这个错误的一般含义是什么?
编译发布版本时不会发生这种情况。只调试构建。
我快速阅读了有关新 chrono 类的 C++ 参考资料,但发现它们有点复杂。
那么,问题是,如何用C++20重写这段代码,来获取年、月、日、时、分、秒?
有什么变化吗?我问这个问题是因为std::localtime:它是线程不安全的。tm将在下次调用后被销毁std::localtime。
std::time_t t = std::time(nullptr);
std::tm *tm = std::localtime(&t);
int year = tm->tm_year + 1900;
int month = tm->tm_mon + 1;
int day = tm->tm_mday;
int hour = tm->tm_hour;
int minute = tm->tm_min;
int second = tm->tm_sec;
Run Code Online (Sandbox Code Playgroud) 我需要一个强有力的保证,int x = (int) std::round(y)始终给出正确的结果(y有限且“人性化”,例如-50000到50000)。
std::round(4.1)可以给4.000000000001或3.99999999999. 在后一种情况下,转换为int给出3,对吧?
为了解决这个问题,我用这个丑陋的函数重新发明了轮子:
template<std::integral S = int, std::floating_point T>
S roundi(T x)
{
S r = (S) x;
T r2 = std::fmod(x, 1);
if (r2 >= 0.5) return r + 1;
if (r2 <= -0.5) return r - 1;
return r;
}
Run Code Online (Sandbox Code Playgroud)
但这有必要吗?或者是否将 from 转换double为int使用最后一个尾数位进行舍入?
我在 g++ 13.2 中得到了奇怪的sizeof()结果。[[no_unique_address]]
#include <iostream>
using namespace std;
template<bool T>
struct B
{
struct Dummy {};
int *a; // swap position with <below placemark>
// In this position, results are "16" and "40".
[[no_unique_address]] std::conditional_t<T, Dummy, size_t> y;
[[no_unique_address]] std::conditional_t<T, Dummy, size_t> x;
[[no_unique_address]] std::conditional_t<T, Dummy, size_t> h;
[[no_unique_address]] std::conditional_t<T, Dummy, size_t> w;
// <below placemark>
// In this position, results are "8" and "40".
};
int main()
{
cout << sizeof(B<true>) << "\n";
cout << …Run Code Online (Sandbox Code Playgroud) 我有一个适配器类,它处理相同概念的类。
现在,我希望该适配器基于模板参数RO(只读)来禁用pack(). 但我不知道纯虚拟是否不支持这一点,或者只是我写错了。
template<bool RO> // means read only functionality
class Adapter
{
struct AbstractAdapter
{
virtual ~AbstractAdapter() = default;
virtual void unpack() = 0;
virtual void pack() = 0 requires (!RO); // compile ERROR!! ERROR!! ERROR!!
};
template<typename T> requires (Reader<T> || Writer<T>)
struct TemplateAdapter : public AbstractAdapter
{
virtual void unpack()
{
if constexpr (!Reader<T>) throw UnsupportedOperationEx{};
else client.unpack();
}
virtual void pack() requires (!RO)
{
if constexpr (!Writer<T>) throw UnsupportedOperationEx{};
else client.pack();
} …Run Code Online (Sandbox Code Playgroud) 当我想要使用函数参数复制语义const T&或使用函数参数移动语义时,如何处理通用引用T&&。后者隐藏了第一个。
下面是带有代数向量运算符的示例代码。
#include <array>
#include <iostream>
template<typename T>
void neg(T &a) { for (auto &i : a) i = -i; }
// object 'a' remains available
template<typename T>
auto operator-(const T &a) { std::cout << "1\r\n"; T b = a; neg(b); return b; }
// object 'a' terminates its life
template<typename T>
auto operator-(T &&a) { std::cout << "2\r\n"; neg(a); return std::move(a); }
// make an rvalue
template<typename T1, typename T2>
auto operator+(const T1 &a, …Run Code Online (Sandbox Code Playgroud) c++ ×10
c++20 ×3
c++11 ×2
templates ×2
c++-concepts ×1
constructor ×1
gcc ×1
lambda ×1
mingw-w64 ×1
pure-virtual ×1
rounding ×1
sfinae ×1
vector ×1