小编Ale*_*sky的帖子

未定义Lambda复制分配运算符

为什么这不能在CLang 7及更低版本中编译,而在CLang 8及更高版本中编译:

#include <map>
#include <string>

typedef std::map<std::string, int> TestMap;

TestMap m {
    {"a", 1},
    {"b", 2},
    {"c", 3},
};

auto func = [](const TestMap::value_type & p) -> int { return p.second; };
auto func1 = func;
//In CLang 7 and lower copy assignment operator is not defined
func = func1;
Run Code Online (Sandbox Code Playgroud)

实际发生了什么变化?

但这可以与所有CLang版本一起编译:

auto func1 = []() { return 5;};
decltype(func1) func2 = func1;
func2 = func1;
Run Code Online (Sandbox Code Playgroud)

这里提供所有示例代码

Lambda之间有什么区别?

c++ clang c++17

5
推荐指数
1
解决办法
113
查看次数

获取一个std :: tuple元素作为std :: variant

给定变量类型:

using Variant = std::variant<bool, char, int, float, double, std::string>;
Run Code Online (Sandbox Code Playgroud)

和一个元组类型,其中包含限于该变量类型的元素(可能有重复和省略,但不能有其他类型):

using Tuple = std::tuple<char, int, int, double, std::string>;
Run Code Online (Sandbox Code Playgroud)

如何在运行时实现通过给定索引获取和设置元组元素的方法为Variant的方法:

Variant Get(const Tuple & val, size_t index);
void Set(Tuple & val, size_t index, const Variant & elem_v);
Run Code Online (Sandbox Code Playgroud)

我的代码中有两个实现,但我有一个更好的印象。我的第一个实现使用std::function第二个构建一个由一些Accessor指针组成的数组,这些指针对移动和复制对象施加了限制(因为它的地址更改了)。我想知道是否有人知道实现此目标的正确方法。

编辑1:

以下示例可能阐明了我的意思:

Tuple t = std::make_tuple(1, 2, 3, 5.0 "abc");
Variant v = Get(t, 1);
assert(std::get<int>(v) == 2);
Set(t, 5, Variant("xyz"));
assert(std::get<5>(t) == std::string("xyz"));
Run Code Online (Sandbox Code Playgroud)

c++ c++17

5
推荐指数
1
解决办法
177
查看次数

从字符串创建 std::chrono::time_point

像这样程序

int
main()
{
    using namespace date;
    std::cout << std::chrono::system_clock::now() << '\n';
}
Run Code Online (Sandbox Code Playgroud)

打印类似的东西2017-09-15 13:11:34.356648

"2017-09-15 13:11:34.356648"假设我的代码中有一个字符串文字。

std::chrono::time_point在 C++20 中创建它的正确方法是什么?

c++ c++20

5
推荐指数
1
解决办法
1517
查看次数

在C++ 11中使用多个模板参数的模板函数的专业化

无法弄清楚为什么我使用以下代码获得编译器错误:

#include <tchar.h>

typedef TCHAR Char;

typedef std::basic_string<Char> String;

template<typename C, typename T>
std::basic_string<C> InternalToString(T val);

template<typename T>
inline std::string InternalToString<char, T>(T val)
{
    return std::to_string(val);
}

template<typename T>
inline std::wstring InternalToString<wchar_t, T>(T val)
{
    return std::to_wstring(val);
}

template<typename T>
inline String ToString(T val)
{
    return InternalToString<Char, T>(val);
}
Run Code Online (Sandbox Code Playgroud)

如果我用MSVC 2017编译代码我得到"错误C2768:'InternalToString':非法使用显式模板参数"

c++ templates template-specialization

4
推荐指数
1
解决办法
1409
查看次数

在C++的编译时检查类型是否为std :: basic_string <T>

我在我的C++代码中将is_string定义如下:

#include <string>

template <typename T>
struct is_string
{
    static const bool value = false;
};

template <class T, class Traits, class Alloc>
struct is_string<std::basic_string<T, Traits, Alloc>>
{
    static const bool value = true;
};

int main()
{
    std::cout << is_string<std::string>::value << std::endl;
    std::cout << is_string<std::wstring>::value << std::endl;

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

对于std :: string和std :: wstring都是如此.

但我需要一个这样的谓词:

is_string<char, std::string>::value //to be true
is_string<char, std::wstring>::value //to be false
is_string<wchar_t, std::string>::value //to be false
is_string<wchar_t, std::wstring>::value //to be true
Run Code Online (Sandbox Code Playgroud)

有可能实现它吗?

c++ templates

4
推荐指数
1
解决办法
495
查看次数

为什么std :: bitset <5> {} [0]不是constexpr?

std :: bitset有constexpr构造函数和constexpr运算符[],所以下面的代码编译成功:

#include <bitset>

typedef std::bitset<5> BitSet;

constexpr BitSet s1;
static_assert(!s1[0]);
Run Code Online (Sandbox Code Playgroud)

为什么以下代码没有?

static_assert(BitSet{}[0]);
Run Code Online (Sandbox Code Playgroud)

c++ c++17

4
推荐指数
1
解决办法
217
查看次数

将std :: variant转换为模板类实例的std :: tuple

transform_v2t 下面代码中的function构建模板类A实例的元组:

template <typename T>
struct A
{
    T val;
};

template <class V, template <class> class T, std::size_t... index>
inline constexpr auto transform_v2t(std::index_sequence<index...>)
{
    return std::make_tuple(T<std::variant_alternative_t<index, V>>() ...);
}

template <class V, template <class> class T>
inline constexpr auto transform_v2t()
{
    return transform_v2t<V, T>(std::make_index_sequence<std::variant_size_v<V>>());
}

typedef std::variant<bool, char, int, float, double, std::string> V;

int main()
{
    auto t1 = transform_v2t<V, A>();
}
Run Code Online (Sandbox Code Playgroud)

是否可以将相同的transform_v2t函数应用于具有两个模板参数的类,例如:

template <typename P, typename T>
struct B
{
    P other_val;
    T val;
};
Run Code Online (Sandbox Code Playgroud)

与P专业化为 …

c++ c++17

4
推荐指数
1
解决办法
72
查看次数

How standard containers allocates memory for their nodes (internal structures)?

I cannot figure out how std::set and std::map, for example allocate the memory for their nodes if they have allocators of type

std::allocator<Key> 
Run Code Online (Sandbox Code Playgroud)

and

std::allocator<std::pair<const Key, T> > 
Run Code Online (Sandbox Code Playgroud)

respectively. As far as I can guess, there should be a code like this, in std::set, for example:

std::pair<iterator, bool> insert(const value_type& value)
{
    ...
    Node * node = new Node();
    node->value = value;
    ...
    return InsertNode(node);
 }
Run Code Online (Sandbox Code Playgroud)

or

std::pair<iterator, bool> insert(const value_type& value)
{
    ...
    Node * node = new Node(); …
Run Code Online (Sandbox Code Playgroud)

c++

4
推荐指数
2
解决办法
151
查看次数

如何关闭 docker 容器内的 oracle 数据库?

我从官方 dockerfile运行 Oracle Database 12.2.0.1。据我所知,如果我这样做

docker stop <container_id>
Run Code Online (Sandbox Code Playgroud)

数据库的当前状态丢失,下次它将做一些干净的启动。

如何正确关闭数据库并停止容器,但保存当前状态?

如果我做

./sqlplus sys as sysdba
SHUTDOWN IMMEDIATE
Run Code Online (Sandbox Code Playgroud)

容器仍在运行并且仍然消耗 11GB 的 16GB RAM,所以据我所知,为了停止容器,我可能应该杀死一些进程,但不清楚我应该什么时候做

docker commit <container_id>
Run Code Online (Sandbox Code Playgroud)

所以理想情况下我需要类似 shutdown_oracle_and_commit_container.sh 的东西。

在 docker 容器中,oracle 实例以 runOracle.sh 启动,但没有 stopOracle.sh

oracle docker

3
推荐指数
1
解决办法
1559
查看次数

使可选模板参数成为朋友?

有一篇文章解释了可以使用以下语法将模板参数声明为friend:

template <typename T>
class A {
   friend T;
};
Run Code Online (Sandbox Code Playgroud)

但是如果在某些情况下A需要油炸而在其他情况下则不需要油炸怎么办?是否可以将T作为可选参数?

是否有比使用某种FakeClass作为T更好的解决方案?

EDIT1:我找到了另一个解决方案:

    class B {};

    template <typename T>
    class A {
       friend T;
    };

    template <>
    class A<void> {
    };

int main()
{
    A<B> a1;
    A<void> a2;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

但是如果A是具有300行代码的复杂类怎么办?是否有没有模板专门化的替代解决方案?

c++ c++17

3
推荐指数
1
解决办法
67
查看次数

标签 统计

c++ ×9

c++17 ×5

templates ×2

c++20 ×1

clang ×1

docker ×1

oracle ×1

template-specialization ×1