小编abr*_*ert的帖子

可调用结果类型的扣除

我尝试推断可调用模板参数的类型,不幸的是没有成功:

template<typename callable, typename T_out >
class A
{};

template<typename callable>
auto make_A( callable f )
{
  return A<callable, typename std::result_of_t<callable> >{ f };
}

int main()
{
  make_A( []( float f ){ return f;} );
}
Run Code Online (Sandbox Code Playgroud)

上面的代码导致以下错误:

error: implicit instantiation of undefined template 'std::__1::result_of<(lambda at /Users/arirasch/WWU/dev/xcode/tests/tests/main.cpp:31:11)>'
template <class _Tp> using result_of_t = typename result_of<_Tp>::type;
Run Code Online (Sandbox Code Playgroud)

有谁知道如何修理它?

提前谢谢了。

c++ callable type-deduction c++14

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

模板参数的乘法

我有以下问题:

template< size_t... N_i >
class A
{
  // ...
  std::array< float, /* product of the unpacked elements of N_i */ > arr;
};
Run Code Online (Sandbox Code Playgroud)

如上所示,我尝试声明一个std::array名为arr一个类的成员A.在这里,我想要arr具有解包元素N_i的大小产品,例如,在A<2,3,4>"arr"的情况下应该具有大小2*3*4=24.
有谁知道这是如何实现的?

c++ templates variadic-templates stdarray

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

使用算术运算符时禁止隐式转换

我们如何告诉 C++ 编译器在使用+和这样的算术运算符时应该避免隐式转换/,即,

size_t st_1, st_2;
int    i_1,  i_2;

auto st = st_1 + st_2; // should compile
auto i  = i_1  + i_2;  // should compile

auto error_1 = st_1 + i_2;  // should not compile
auto error_2 = i_1  + st_2; // should not compile
// ...
Run Code Online (Sandbox Code Playgroud)

c++ arithmetic-expressions implicit-conversion

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

将int转换为size_t

当我传递integer给一个时,我想知道clang编译器的以下警告std::initializer_list< size_t >:

non-constant-expression cannot be narrowed from type 'int' to 'unsigned long' in initializer list
Run Code Online (Sandbox Code Playgroud)

为什么可以int转换为a size_tint不能传递给a std::initializer_list< size_t >,即

int main()
{
  size_t s_t = 0;
  int    i   = 0;

  std::initializer_list<size_t> i_l = { i };            // warning
  s_t = i;                                              // no warning

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

c++ casting initializer-list

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

了解通用引用的类型推导

让我们foo的功能:

template< typename T >
void foo( T&& a ){}
Run Code Online (Sandbox Code Playgroud)

T以下调用将推断出什么类型foo:

foo( 0 ); // is T here int or int&& ?

int a = 0;
foo( a ); // is T here int or int& ?
Run Code Online (Sandbox Code Playgroud)

c++ templates function type-deduction forwarding-reference

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

Deduce模板参数,用于初始化列表的大小

我有以下(不可编译)代码:

template< size_t N >
void foo( std::array<int, N> )
{
  // Code, where "N" is used.
}

int main()
{
  foo( { 1,2 } );
}
Run Code Online (Sandbox Code Playgroud)

在这里,我想将任意数量的ints传递给函数foo- 为方便起见,我将使用std::initializer_list符号.我尝试使用a std::array来聚合ints(如上面的代码所示),但是,编译器无法推断出数组大小,因为ints是作为一个传递的std::initializer_list.

使用an std::initializer_list而不是std::array也不能解决问题,因为(与此相反std::array)std::initializer_list未捕获的大小不作为模板参数.

有谁知道哪些数据结构可以使用,以至于intS能够通过传递std::initializer_list符号和不通过模板参数Nfoo明确?

提前谢谢了

c++ initializer-list template-argument-deduction c++14

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

我们需要移动和复制分配吗?

对于课程A,我们可以使用

A& operator=( A other ) { /* ... */ }
Run Code Online (Sandbox Code Playgroud)

代替

A& operator=( const A&  other ) { /* ... */ }
A& operator=( const A&& other ) { /* ... */ }
Run Code Online (Sandbox Code Playgroud)

没有恶化的表现或其他负面影响?

c++ assignment-operator move-semantics c++11

3
推荐指数
2
解决办法
244
查看次数

调用模板函数时出现歧义

我有以下问题:

template< typename T >
class A
{};

class B
{
  public:
    template< typename... T >
    void operator()( A<T>... a )
    {
      std::cout << "A<T>\n";
    }

    template< typename callable >
    void operator()( callable f )
    {
      std::cout << "callable\n";
    }
};


int main()
{
  B      b;
  A<int> a;

  b( a );
}
Run Code Online (Sandbox Code Playgroud)

调用b( a )是模糊的 - 我期望输出A<T>,即执行第一个定义operator().有谁知道如何修理它?

c++ templates ambiguity variadic-templates c++14

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

r值在不使用std :: move的情况下引发警告

有人可以帮助我理解为什么以下代码会导致警告

struct A
{
  A() : _a( 0 ) {}

  const int& _a;
};


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

有警告

warning: binding reference member '_a' to a temporary value [-Wdangling-field]
      A() : _a( 0 ) {}
Run Code Online (Sandbox Code Playgroud)

但是这段代码std::move用于初始化成员_a,不会:

struct A
{
  A() : _a( std::move(0) ) {}

  const int& _a;
};


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

是不是0std::move( 0 )两个r值?

c++ rvalue-reference move-semantics c++14

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

使用`std :: conditional_t`来根据其模板参数定义类'typedef`

我尝试使用std::conditional_t来定义一个类Atypedef在它的模板参数的依赖关系T

template< typename T >
class A
{
  public:
    typedef std::conditional_t< std::is_fundamental<T>::value, T, decltype(std::declval<T>().foo())> type;
};

template< typename T >
class B
{
  public:
    T foo()
    {
      // ...
    }
};


int main()
{
  typename A< int >::type a = 5;    // causes an error
  typename A< B<int> >::type b = 5; // does not cause an error

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

不幸的是,代码无法编译。错误:

error: member reference base type 'int' is not a structure …
Run Code Online (Sandbox Code Playgroud)

c++ templates c++14

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

非虚函数可以等于0吗?

非虚函数可以等于0,例如,类似的东西

void foo() = 0
Run Code Online (Sandbox Code Playgroud)

关键字"虚拟"不在前面的位置?

最好

c++ virtual

0
推荐指数
2
解决办法
470
查看次数