我尝试推断可调用模板参数的类型,不幸的是没有成功:
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)
有谁知道如何修理它?
提前谢谢了。
我有以下问题:
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++ 编译器在使用+和这样的算术运算符时应该避免隐式转换/,即,
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) 当我传递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_t但int不能传递给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) 让我们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) 我有以下(不可编译)代码:
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符号和不通过模板参数N的foo明确?
提前谢谢了
对于课程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)
没有恶化的表现或其他负面影响?
我有以下问题:
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().有谁知道如何修理它?
有人可以帮助我理解为什么以下代码会导致警告
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)
是不是0和std::move( 0 )两个r值?
我尝试使用std::conditional_t来定义一个类A的typedef在它的模板参数的依赖关系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)