小编Nic*_*las的帖子

为什么我的类不可默认构造?

我有这些课程:

#include <type_traits>

template <typename T>
class A {
public:
    static_assert(std::is_default_constructible_v<T>);

};

struct B {
   struct C {
      int i = 0;
   };

    A<C> a_m;
};

int main() {
    A<B::C> a;
}
Run Code Online (Sandbox Code Playgroud)

编译时,a_m不是默认可构造的,而是a可构造的。

更改C为:

struct C {
      int i;
   };
Run Code Online (Sandbox Code Playgroud)

一切安好。

使用 Clang 9.0.0 测试。

c++ nsdmi

29
推荐指数
1
解决办法
960
查看次数

为什么我的访问者的 std::variant 堆栈损坏

我很难理解这段代码发生了什么:


#include <iostream>
#include <variant>

template <class... Ts>
struct overloaded : Ts... {
    using Ts::operator()...;
};
template <class... Ts>
overloaded(Ts...) -> overloaded<Ts...>;

struct FirstPSet {
    std::uint16_t uid;
};

struct SecondPSet {
    std::uint16_t uid;
};

using PSet = std::variant<std::monostate, FirstPSet, SecondPSet>;

auto getUID(const PSet& pSet) {
    auto visitor = overloaded{[](const std::monostate&) -> std::uint16_t { return 0; },
                              [](const auto& pSet) -> std::uint16_t { return pSet.uid; }};
    return std::visit(visitor, pSet);
}

int main() {
    FirstPSet pSet{42};

    std::cout << getUID(pSet) << '\n';
} …
Run Code Online (Sandbox Code Playgroud)

c++ stack visual-studio visual-c++ c++17

7
推荐指数
0
解决办法
116
查看次数

其内部的静态模板类

我们有这样的代码:

template <typename T>
struct A {
    static constexpr A a = A{};
};


template <typename T>
struct B {
    T a;
};

B<A<int>> b;
Run Code Online (Sandbox Code Playgroud)

GCC 13 似乎对 L.3 感到满意,但对 clang 16 不满意。

<source>:3:21: error: constexpr variable cannot have non-literal type 'const A<int>'
        static constexpr A a = A{};
                           ^
<source>:9:4: note: in instantiation of template class 'A<int>' requested here
        T a;
          ^
<source>:12:11: note: in instantiation of template class 'B<A<int>>' requested here
B<A<int>> b;
          ^
<source>:3:21: note: incomplete type …
Run Code Online (Sandbox Code Playgroud)

c++ templates struct language-lawyer

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

调用system()函数,Vim脚本

当我遇到问题时,我试图在Vim 7.3中自定义我的状态行.

我正在尝试将SVN信息放在状态行中,所以我做了类似这样的事情:

function! DrawStatusLine()
    let svn = system("svn info")
    let l:status = " "
    let l:status = l:status . svn
    let l:status = l:status . "%t"       "tail of the filename
    let l:status = l:status . "%*"
    let l:status = l:status . "[%{strlen(&fenc)?&fenc:'none'}," "file encoding
    let l:status = l:status . "%{&ff}]" "file format
    let l:status = l:status . "%h"      "help file flag
    let l:status = l:status . "%m"      "modified flag
    let l:status = l:status . "%r"      "read only flag
    let …
Run Code Online (Sandbox Code Playgroud)

svn vim

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

标签 统计

c++ ×3

c++17 ×1

language-lawyer ×1

nsdmi ×1

stack ×1

struct ×1

svn ×1

templates ×1

vim ×1

visual-c++ ×1

visual-studio ×1