小编0xB*_*F00的帖子

什么时候使用"身份"tmp技巧?

我已经看到过使用这个元函数,但从未真正理解为什么以及在什么情况下需要它.有人能用一个例子解释一下吗?

template <typename T>
struct identity
{
    using type = T;
};
Run Code Online (Sandbox Code Playgroud)

c++

18
推荐指数
2
解决办法
1320
查看次数

实用的C++元编程

我刚读过"Practical C++ Metaprogramming"这本书,它有以下我无法编译的例子.你能帮忙解决这个问题.

template <typename F>
struct make_tuple_of_params;

template <typename Ret, typename... Args>
struct make_tuple_of_params<Ret (Args...)>
{
   using type = std::tuple<Args...>;
};

template <typename F>
using make_tuple_of_params_t = typename make_tuple_of_params<F>::type;

template<typename F>
void some_magic_function(F callable)
{
   make_tuple_of_params_t<F> tuple;
   /*
    ... do something with arguments in tuple...
   */
}

int main()
{
   some_magic_function([] (int, double, float) {});
}
Run Code Online (Sandbox Code Playgroud)

我收到一个编译错误说:'type'不是'make_tuple_of_params'的任何直接或间接基类的成员.由于选择了默认结构,因此SFINAE无法按预期工作.我该如何解决?

c++ template-meta-programming variadic-templates c++14

8
推荐指数
2
解决办法
1262
查看次数

如何使用 const/non 模拟重载函数

我如何模拟以下代码?

class ISomeClass
{
public:
   virtual ~ISomeClass() {} = 0;
   virtual const MyType & getType() const = 0;
   virtual MyType & getType() = 0;
};
Run Code Online (Sandbox Code Playgroud)

我尝试了以下方法,但不起作用。你能帮我么?

class MockSomeClass : public ISomeClass
{
public:
    using MyTypeConstRefType = const MyType&;
    using MyTypeRefType = MyType&;

public:
    MOCK_METHOD0(getType, MyTypeConstRefType(void) const);

    MOCK_METHOD0(getType, MyTypeRefType(void));
};
Run Code Online (Sandbox Code Playgroud)

c++ googlemock

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

如果constexpr而不是标签调度

我想使用if constexpr而不是标签调度,但我不知道如何使用它.示例代码如下.

template<typename T>
struct MyTag
{
   static const int Supported = 0;
};

template<>
struct MyTag<std::uint64_t>
{
  static const int Supported = 1;
};

template<>
struct MyTag<std::uint32_t>
{
  static const int Supported = 1;
};

class MyTest
{
public:
   template<typename T>
   void do_something(T value)
   {
      // instead of doing this
      bool supported = MyTag<T>::Supported;

      // I want to do something like this
      if constexpr (T == std::uint64_t)
          supported = true;
   }
};
Run Code Online (Sandbox Code Playgroud)

c++ templates c++17 if-constexpr

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

肖恩·帕特(Sean Parent):对于继承层次结构中的多态类型,具有可变对象是极端例外

我想知道肖恩家长,其实就是 被这句话

对于继承层次结构中的多态类型,具有可变对象是极端例外...

他继续提到两个原因,但是我很难理解他的解释。

是什么让我无法在子类中提供实际上更改此对象内部结构的函数?

有人可以详细说明一下吗?

c++ polymorphism inheritance virtual-functions c++11

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

Grafana 的 iframe 显示“拒绝连接”

我正在使用 grafana 版本 6.5.3,我正在尝试使 grafana 仪表板显示在我的 blazor 服务器应用程序网页上的 iframe 中。iframe 已呈现,但显示以下消息“xxx.xxx.com 拒绝连接”。

<iframe src="http://xxx.xxx.com/mygrafanacharts"
     frameborder="0">
</iframe>
Run Code Online (Sandbox Code Playgroud)

Grafana 服务器配置为

allow_embedding = true
Run Code Online (Sandbox Code Playgroud)

我不知道 Grafana 服务器是否配置不正确,或者我的网站上是否有一些配置需要更改。

iframe grafana blazor

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

std :: variant vs std :: any当type是move constructible时

如果我有一个类型,std::is_nothrow_move_constructible我需要将它存储在一个std::anystd::variant哪个,你会建议使用哪个,为什么?哪一个会产生最少的开销?编辑:std::variantvs 的不同用例有哪些std::any

class MyType
{
public:
   MyType(const MyType&) = default;
   MyType(MyType&&) = default;
   MyType() = default;
};

int main(int argc, char* argv[])
{
   static_assert(std::is_nothrow_move_constructible<MyType>::value, "Not move constructible");
   return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ performance types c++17

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