我有以下问题:
template< size_t... N_i >
class A
{
public:
// ...
void foo()
{
bar( /* 0,...,0 <- sizeof...(N_i) many */);
}
};
Run Code Online (Sandbox Code Playgroud)
我想调用一个函数并向它bar传递sizeof...(N_i)许多参数,这些参数都是零,例如,bar(0,0,0)以防万一sizeof...(N_i) == 3.如何实施?
我必须遵循以下问题:
template< size_t... N_i >
class A
{
// ...
};
template< size_t N, size_t... N_i >
A</* first N elements of N_i...*/> foo()
{
A</* first N elements of N_i...*/> a;
// ...
return a;
}
int main()
{
A<1,2> res = foo<2, 1,2,3,4>();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在这里,我想要foo返回类型A</* first N size_t of N_i...*/>,即class A具有参数包的前N个元素作为模板参数N_i.
有谁知道这是如何实现的?
我尝试A按如下方式定义一个类:
template< typename T >
class A
{
public:
A( T elem )
: _elem( elem )
{}
private:
TYPE _elem; // "TYPE" should be either "T" in case "elem" is an r-value or "T&" in case "elem" is an l-value.
};
Run Code Online (Sandbox Code Playgroud)
在这里,我希望_elem有一个类型T,以防构造函数的参数elem是一个r值或者T&case中的类型elem是一个l值.
有谁知道这是如何实现的?
我有以下问题:
template< typename callable, typename T , size_t... N_i>
void foo()
{
using callable_out_type = std::result_of_t< callable( /* T , ... , T <- sizeof...(N_i) many */ ) >;
// ...
}
Run Code Online (Sandbox Code Playgroud)
我想获得结果类型,callable其中将类型的sizeof...(N_i)许多参数T作为其输入,例如,callable(1,2,3)在T==int和的情况下sizeof...(N_i)==3.如何实施?
提前谢谢了.
我有以下问题:
template< std::size_t N >
class A
{
std::function< std::size_t( /*std::size_t,....,std::size_t <- N-times*/) > foo;
};
Run Code Online (Sandbox Code Playgroud)
正如您在上面所看到的,我尝试将a声明std::function<...> foo为类的成员A.在这里,我希望foo具有返回类型std::size_t(这没有问题)并且作为输入,我将传递N次类型std::size_t但我不知道如何.有可能吗?
提前谢谢了.
关于以下代码:
void foo( int in )
{
std::cout << "no ref" << std::endl;
}
void foo( int&& in )
{
std::cout << "ref&&" << std::endl;
}
int main()
{
foo( static_cast<int&&>(1) );
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我想知道为什么呼叫foo( static_cast< int&& >(1) )是模棱两可的:
error: call to 'foo' is ambiguous
foo( static_cast<int&&>(1) );
^~~
Run Code Online (Sandbox Code Playgroud)
由于显式强制转换int&&,我预计void foo( int&& in )会被调用.
我有以下问题:
#include <vector>
#include <tuple>
using namespace std;
template< size_t... N_i, typename Ts... >
class A
{
// ...
private:
std::vector<size_t> _v = { N_i... };
std::tuple<Ts...> _t;
};
int main()
{
A<1> a;
}
Run Code Online (Sandbox Code Playgroud)
如您所见,我尝试将多个参数包定义为类的模板参数A.
不幸的是,代码无法编译:
错误:'Ts'之前的预期嵌套名称说明符
如何为此示例定义多个参数包?
如何为班级成员使用自动类型扣除?例如,以下代码
struct A
{
auto foo(); // foo is defined in another file
};
int main()
{
A a;
a.foo();
}
Run Code Online (Sandbox Code Playgroud)
其中foo返回类型auto导致以下错误:
error: function 'foo' with deduced return type cannot be used before it is defined
a.foo();
^
Run Code Online (Sandbox Code Playgroud)
错误是可以理解的,因为编译foo在不知道其定义的情况下无法知道返回类型是什么.
我的问题是,如果有任何变通方法或某种编程模式来规避自动返回类型不能用于类成员函数的问题,以防函数的声明和定义被分开.
有人可以帮助我理解为什么下面的代码会导致错误吗?
class A
{
public:
float& operator()()
{
return _f;
}
private:
float _f = 1;
} a;
auto& foo()
{
std::function<float()> func = a;
return func();
}
int main()
{
std::cout << foo() << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
错误:
error: non-const lvalue reference to type 'float' cannot bind to a temporary of type 'float'
return func();
^~~~~~
1 error generated.
Run Code Online (Sandbox Code Playgroud)
在这里,operator()我返回一个参考_f,因此,我认为func()这不是暂时的.如果有人帮助我理解会很棒.
有人知道为什么编译时没有警告吗
int main()
{
const int i = 1024;
std::initializer_list<size_t> i_l = { i }; // no warning
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但不
int main()
{
const int i = pow(2,10);
std::initializer_list<size_t> i_l = { i }; // warning
return 0;
}
Run Code Online (Sandbox Code Playgroud)
警告:
non-constant-expression cannot be narrowed from type 'int' to 'unsigned long' in initializer list [-Wc++11-narrowing]
std::initializer_list<size_t> i_l = { i }; i_l = i_l; // warning
Run Code Online (Sandbox Code Playgroud) c++ ×10
c++14 ×7
std-function ×2
templates ×2
auto ×1
callable ×1
class ×1
lvalue ×1
narrowing ×1
overloading ×1