相关疑难解决方法(0)

可变构造函数是否应该隐藏隐式生成的构造函数?

可变构造函数是否应该隐藏隐式生成的构造函数,即默认构造函数和复制构造函数?

struct Foo
{
    template<typename... Args> Foo(Args&&... x)
    {
        std::cout << "inside the variadic constructor\n";
    }
};

int main()
{
    Foo a;
    Foo b(a);
}
Run Code Online (Sandbox Code Playgroud)

不知何故,我希望在阅读这个答案之后不打印任何内容,但它会inside the variadic constructor在g ++ 4.5.0上打印两次:(这种行为是否正确?


它也没有可变参数模板:

struct Foo
{
    Foo()
    {
        std::cout << "inside the nullary constructor\n";
    }

    template<typename A> Foo(A&& x)
    {
        std::cout << "inside the unary constructor\n";
    }
};

int main()
{
    Foo a;
    Foo b(a);
}
Run Code Online (Sandbox Code Playgroud)

同样,两行都打印出来.

c++ templates copy-constructor variadic-templates c++11

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

(Im)使用可变参数模板完美转发

概要

给定具有可变参数模板构造函数的类型将参数转发给实现类,是否可以限制使用SFINAE转发的类型?

细节

首先,考虑带有通用引用的构造函数的非变量情形.这里可以禁止通过SFINAE转发非常量左值引用来代替使用复制构造函数.

struct foo
{
  foo() = default;

  foo(foo const&) 
  {
      std::cout << "copy" << std::endl;
  }

  template <
    typename T,
    typename Dummy = typename std::enable_if<
      !std::is_same<
          T,
          typename std::add_lvalue_reference<foo>::type
      >::value
    >::type
  >
  foo(T&& x)
    : impl(std::forward<T>(x))
  {
      std::cout << "uref" << std::endl;
  }

  foo_impl impl;
};
Run Code Online (Sandbox Code Playgroud)

这种通用引用的限制很有用,因为否则实现类将接收类型的非const左值引用foo,它不知道.LWS的完整示例.

但是,如何使用可变参数模板?有可能吗?如果是这样,怎么样?天真的扩展不起作用:

template <
  typename... Args,
  typename Dummy = typename std::enable_if<
    !std::is_same<
        Args...,
        typename std::add_lvalue_reference<foo>::type
    >::value
  >::type
>
foo(Args&&... args)
  : impl(std::forward<Args>(args)...)
{
    std::cout << "uref" …
Run Code Online (Sandbox Code Playgroud)

c++ variadic-functions sfinae variadic-templates c++11

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

在C++ 17中,"std :: is_callable"是否替换为"std :: is_invocable"?

cppref已删除了入口页面std::is_callable,并std::is_invocable改为使用入口页面.

但是,std::is_callable仍可在Visual Studio 2017中使用.

std::is_callable正式[ 取出更换| |不赞成用] std::is_invocable在C++ 17?

c++ standards language-design type-traits c++17

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

为什么可变参数模板构造函数比复制构造函数更好?

以下代码无法编译:

#include <iostream>
#include <utility>

struct Foo
{
    Foo() { std::cout << "Foo()" << std::endl; }
    Foo(int) { std::cout << "Foo(int)" << std::endl; }
};

template <typename T>
struct Bar
{
    Foo foo;

    Bar(const Bar&) { std::cout << "Bar(const Bar&)" << std::endl; }

    template <typename... Args>
    Bar(Args&&... args) : foo(std::forward<Args>(args)...)
    {
        std::cout << "Bar(Args&&... args)" << std::endl;
    }
};

int main()
{
    Bar<Foo> bar1{};
    Bar<Foo> bar2{bar1};
}
Run Code Online (Sandbox Code Playgroud)

编译器错误告诉我编译器试图使用variadic模板构造函数而不是复制构造函数:

prog.cpp: In instantiation of 'Bar<T>::Bar(Args&& ...) [with Args = {Bar<Foo>&}; T = …
Run Code Online (Sandbox Code Playgroud)

c++ templates overload-resolution variadic-templates c++11

10
推荐指数
2
解决办法
753
查看次数

为什么模板复制构造函数会覆盖默认的复制构造函数?

编辑更新的代码:

class Any
{
public:
    Any()
    {
    }

    Any(const Any &other)
    {

    }

    Any(Any &other) // added per Ben's answer
    {
    }

    Any(Any &&other)
    {
    }

    Any(const char *value)
    {
    }

    template<typename T>
    Any(const T &value)
    {
    }

    template<typename T>
    Any(T &&value)
    {
        cout << "move ctor" << endl;
    }

    template<typename T>
    Any(const vector<T> &value)
    {
    }

    template<typename T>
    Any(vector<T> &&value)
    {
    }
};

int main(int argc, char *argv[])
{
    vector<string> numbers;
    numbers.push_back("one");
    numbers.push_back("two");
    numbers.push_back("three");
    numbers.push_back("four");

    Any anyNumbers(numbers);
    Any …
Run Code Online (Sandbox Code Playgroud)

c++ templates

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