编译时 Bimap static_assert失败
Compile time Bimap static_assert failure
我正在尝试使用constexpr
和std::array
进行简单的编译时Bimap。
这似乎适用于第一个示例。但是,对于第二个示例,我有以下静态错误:
错误:static_assert失败"3 == 3.3">
static_assert(meta_dict.find_key_of_value(3( == 3.3, "3 == 3.3"(; 失败
这里是Wandbox上可用的代码。应该编译(上述错误除外(与Clang>=5.0.0
和GCC>=8.0.0
.
#include <utility>
#include <array>
#include <stdexcept>
#include <string_view>
#include <iostream>
template<class Key, class Value, std::size_t N>
struct MetaBimap {
using Item = std::pair<Key, Value>;
constexpr MetaBimap(const std::array<Item, N>& d)
: dictionnary{d}
{}
constexpr Value find_value_of_key(const Key& key, int range = N) const
{
if (range == 0)
throw std::out_of_range("Cannot find key");
return (dictionnary[range - 1].first == key)
? dictionnary[range - 1].second
: find_value_of_key(key, range - 1);
}
constexpr Key find_key_of_value(const Value& value, int range = N) const
{
if (range == 0)
throw std::out_of_range("Cannot find value");
return (dictionnary[range - 1].second == value)
? dictionnary[range - 1].first
: find_key_of_value(value, range - 1);
}
std::array<Item, N> dictionnary;
};
int main()
{
// first example
{
using namespace std::string_view_literals;
constexpr std::array<std::pair<std::string_view, int>, 3> meta_array {{
{ "one"sv, 1 },
{ "two"sv, 2 },
{ "three"sv, 3 }
}};
constexpr MetaBimap<std::string_view, int, 3> meta_dict = meta_array;
// OK on wandbox with Clang>=5.0.0 and GCC>=8.0.0 (though not working on my computer with Clang=5.0.0)
static_assert(meta_dict.find_value_of_key("one"sv) == 1, "one == 1");
static_assert(meta_dict.find_key_of_value(3) == "three"sv, "3 == three");
}
// second example
{
constexpr std::array<std::pair<float, int>, 3> meta_array {{
{ 1.1, 1 },
{ 2.2, 2 },
{ 3.3, 3 }
}};
constexpr MetaBimap<float, int, 3> meta_dict = meta_array;
static_assert(meta_dict.find_value_of_key(1.1) == 1, "1.1 == 1"); // OK
std::cout << meta_dict.find_key_of_value(3) << std::endl;
// static_assert(meta_dict.find_key_of_value(3) == 3.3, "3 == 3.3"); // fails
}
}
为什么这里会出现静态故障以及如何解决?同样作为奖励,有什么方法可以简化bimap的构建吗?
问题可以简化为:
constexpr float a = 3.3;
static_assert (a == 3.3f, "float 3.3 is different from literal compiled 3.3f"); // ok
static_assert (a == 3.3, "float 3.3 is different from literal compiled 3.3"); //this fails
用代码编写的文字 3.3 被编译器视为双精度,当将浮点数与双精度进行比较时,它们具有不同的精度,可能会发生(几乎总是(它们不匹配的情况
使用 3.3f
您告诉编译器,用代码编写的文字应被视为float
,而不指定任何后缀将默认为 double
看看这里
相关文章:
- 如果没有malloc,链表实现将失败
- 模板参数替换失败,并且未完成隐式转换
- 具有默认模板参数的多态类的模板推导失败
- 视图中的参数推导失败:take_while
- 链接到自行创建的dll失败
- 带有特殊路径部分的"std::filesystem::weakly_canonical"失败
- GetShortPathName在网络驱动器上使用中文文件夹时失败
- gcc和c++17的过载解析失败
- 为什么使用 P/Invoke 调用 dll 时,某些计算机中的 LoadLibrary 失败?
- 在WSL:configure_file上对config_file的每次调用都失败:配置文件时出现问题
- 使用 GCC 卸载的 OpenMP 卸载失败,并出现"Ptx assembly aborted due to errors"
- 使用cmake从源代码构建MySQL连接器/C++失败(与以前的声明冲突)
- 链接阶段在Ubuntu上失败,但在MacOS上失败
- 从父数组测试用例构造二叉树失败
- LibGit2 SSH身份验证失败
- boost shared_ptr assert失败,但值不是null
- QTableView clearSelect 失败,并显示 ASSERT:在文件 /usr/include/qt4/QtCore/qlist.h 中"!isEmpty()",第 282 行
- c++: assert()失败是否触发任何信号
- 在单独的线程上删除qt类后ASSERT失败
- assert函数如何在失败时返回文件名和代码行