如何(如果可能的话)我可以使用c ++ 11可变参数编程来定义vector函数体中的一系列函数(或者换句话说,一个N具有递减N的s 的序列直到0),如下面的变量?
vector<vector<vector<int>>> v<3>;
vector<vector<int>> v<2>;
vector<int> v<1>;
int v<0>;
Run Code Online (Sandbox Code Playgroud)
我想象的是:
#include <iostream>
#include <vector>
using namespace std;
template<int ...> struct seq {};
template<int N, int ...S> struct gens : gens<N-1, N-1, S...> {};
template<int ...S> struct gens<0, S...>{ typedef seq<S...> type; };
template<int ...S>
void f(seq<S...>) {
//how do I write definitions of v<N> here?
vector<vector<...(N layers)<vector<int> ...> v<N>; //??how-to, not valid c++
vector<vector<...(N -1 layers)<vector<int> ...> v<N-1>;//??how-to, not valid c++ …Run Code Online (Sandbox Code Playgroud) 我想修改现有的类构造函数:
template< typename T, typename... Ts >
MyClass( std::vector<T>& head, Ts& ...tail );
Run Code Online (Sandbox Code Playgroud)
这样就可以指定处理标志:
template< typename T, typename... Ts >
MyClass( MyEnum myEnum, std::vector<T>& head, Ts& ...tail );
Run Code Online (Sandbox Code Playgroud)
这工作正常,但是我想知道是否有一种方法可以将它指定为最右边的参数,并且可能具有默认值.我从来没有见过像这样声明的变量模板,但是再一次,我找不到任何明确说明它们不可能的东西.我试过了:
template< typename T, typename... Ts >
MyClass( std::vector<T>& head, Ts& ...tail, MyEnum myEnum );
...
MyClass myClass( dataA, dataB, dataC, MyEnum::VALUE );
Run Code Online (Sandbox Code Playgroud)
但编译器不喜欢它,我假设它是由于可变参数模板如何解析而且它们必须是最右边的参数?
这在C++ 11中是否可行?
以下代码使用g ++ 7.3.0成功编译,无法使用clang ++ 6.0.0进行编译(编译标志为-std=c++17 -Wall -Wextra -Werror -pedantic-errors):
auto foo = [](auto, auto... tail) {
if constexpr (sizeof...(tail) > 0)
{
return foo(tail...);
}
else
{
return 42;
}
};
int main()
{
}
Run Code Online (Sandbox Code Playgroud)
clang ++编译错误信息:
错误:使用推导类型'auto'声明的变量'foo'不能出现在自己的初始化程序中
Run Code Online (Sandbox Code Playgroud)return foo(tail...);
在这种情况下,符合标准的行为是什么?
假设我们有两种类型(完整和不完整):
struct CompleteType{};
struct IncompleteType;
Run Code Online (Sandbox Code Playgroud)
我们还有模板代码:
#include <type_traits>
template <typename = X(T)>
struct Test : std::false_type {};
template <>
struct Test<T> : std::true_type {};
Run Code Online (Sandbox Code Playgroud)
T可以是CompleteType或IncompleteType在这里X(T)可以T,decltype(T())或者decltype(T{})(假设X(T)是一个宏).
此代码以下列方式使用:
std::cout << std::boolalpha << Test<>::value << std::endl;
Run Code Online (Sandbox Code Playgroud)
下面你可以看到不同的编译器如何处理这样的代码:
铿锵3.4
X(T) \ T CompleteType IncompleteType
T true true
decltype(T()) true --- (1, 2)
decltype(T{}) true --- (1, 2)
Run Code Online (Sandbox Code Playgroud)
error: invalid use of incomplete type 'IncompleteType'甚至在模板类声明中给出了不完整类型(对于decltype(T())和decltype(T{}),但不是为了简单 …
我正在尝试使用来自Eigen 3的Matrix类作为我的状态向量来利用Boost的ODE集成功能,但我遇到了Boost深入的问题,我不明白如何解决.
我正在尝试做的最小例子:
#include <Eigen/Core>
#include <boost/numeric/odeint/stepper/runge_kutta_dopri5.hpp>
#include <iostream>
using namespace Eigen;
using namespace boost::numeric::odeint;
template<size_t N>
using vector = Matrix<double, N, 1>;
typedef vector<3> state;
int main() {
state X0;
X0 << 1., 2., 3.;
state xout = X0;
runge_kutta_dopri5<state> stepper;
// If I remove these lines, everything compiles fine
stepper.do_step([](const state x, state dxdt, const double t) -> void {
dxdt = x;
}, X0, 0.0, xout, 0.01);
std::cout << …Run Code Online (Sandbox Code Playgroud) 以下代码抛出错误消息,我无法弄清楚问题是什么 - 是这个词static,还是const?我究竟做错了什么?
#include <iostream>
using namespace std;
class SampleClass
{
private:
int value;
static int counter;
public:
SampleClass(int i)
{
value = i;
counter++;
}
static int countSomeClass() const
{
return counter;
}
void showValue()
{
cout << value << endl;
}
};
int main()
{
SampleClass test(50);
test.showValue();
test.countSomeClass();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
错误信息:
main.cpp:16:35:错误:静态成员函数static int SampleClass :: countSomeClass()不能有cv-qualifier
static int countSomeClass()const
在下面的C++ 11 +代码中,返回语句构造应该是首选吗?
#include <utility>
struct Bar
{
};
struct Foo
{
Bar bar;
Bar get() &&
{
return std::move(bar); // 1
return bar; // 2
}
};
Run Code Online (Sandbox Code Playgroud) 我有一个函数at旨在通过运行时指定的索引访问std :: tuple元素
template<std::size_t _Index = 0, typename _Tuple, typename _Function>
inline typename std::enable_if<_Index == std::tuple_size<_Tuple>::value, void>::type
for_each(_Tuple &, _Function)
{}
template<std::size_t _Index = 0, typename _Tuple, typename _Function>
inline typename std::enable_if < _Index < std::tuple_size<_Tuple>::value, void>::type
for_each(_Tuple &t, _Function f)
{
f(std::get<_Index>(t));
for_each<_Index + 1, _Tuple, _Function>(t, f);
}
namespace detail { namespace at {
template < typename _Function >
struct helper
{
inline helper(size_t index_, _Function f_) : index(index_), f(f_), count(0) {}
template < typename _Arg …Run Code Online (Sandbox Code Playgroud) 虽然我试图理解为什么代码int(*)();编译好用g ++编译我发现更奇怪的事情:
int main()
{
int(*){} Is it C++11 or any other language?
}
Run Code Online (Sandbox Code Playgroud)
这段代码用g ++ 4.8.1编译得很好,参见live example(flags :)-std=c++11 -pedantic-errors.clang 3.4和vc ++ 2013都没有编译它.
它是一种新的C++ 11样式的注释,只有g ++支持吗?还是编译错误?
以下是我发现的关于这种"评论"风格的内容:
注释的通常结构(可以省略的部分括在括号中[ ... ]):
int // or another valid C++ type
(*) // or another sequence of '*' and/or '&' characters with nonzero length
{"[Comment header]"}
[Comment body]
{[Comment footnote]}
Run Code Online (Sandbox Code Playgroud)而不是使用脚注终止符号;可以使用:int(*){} Comment …
我们考虑一组模板别名:
template<class T> using foo = T*;
template<class T> using bar = T*;
template<class T> using buz = foo<T>;
template< template<class>class TT > struct id {};
using id_foo = id<foo>;
using id_bar = id<bar>;
using id_buz = id<buz>;
Run Code Online (Sandbox Code Playgroud)
是id_foo,id_bar,id_buz相同或不同类型的?是foo,bar,buz相同或不同的模板?
各种编译器对此有不同的看法.尤其,
buz与...相同foo第14.5.7章"别名模板"中的标准C++ 11目前还不清楚.