小编K-b*_*llo的帖子

Variadic宏,其variadic参数没有参数

调用M其可变参数的参数没有参数的可变参数宏是否合法?

相关标准报价为[cpp.replace]/4:

如果宏定义中的标识符列表不以省略号结尾,则在调用类函数宏时,参数的数量(包括那些不包含预处理标记的参数)应等于宏定义中的参数数量.否则,调用中的参数应该多于宏定义中的参数(不包括...).应该存在一个)终止调用的预处理标记.

对于没有非可变参数的情况,表单中的调用M()应该是合法的,因为调用有一个参数(不包含预处理标记).因此,除了非变量参数之外还有一个参数.

对于具有一个非可变参数的情况,是否应该有一个尾随,,M(1,)以引入一个由variadic参数组成的参数?否则,参数的数量将等于非可变参数的数量.即

#define variadic(x,...) #__VA_ARGS__

variadic(1,) // 2 arguments: ok
variadic(1) // 1 argument: wrong?
Run Code Online (Sandbox Code Playgroud)

但是,ClangGCC都接受以下测试用例:

#include <iostream>

#define variadic(x,...) #__VA_ARGS__

int main()
{
    std::cout << "'" variadic(1) "'" << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

并作为输出产生:

''
Run Code Online (Sandbox Code Playgroud)

这是非标准行为吗?

c++ language-lawyer c-preprocessor variadic-macros c++11

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

C++ 11从参数包创建静态数组

是否可以static const使用模板参数包中的值创建数组?我尝试了以下代码,但gcc 4.8.1给出了"错误:参数包未展开"

template<int... N>
struct ARRAY_OF_DIMS
{
    static constexpr size_t NDIM = sizeof...(N);
    static const int DIMS[NDIM];
};

template<int... N>
const int ARRAY_OF_DIMS<N>::DIMS[ARRAY_OF_DIMS<N>::NDIM] = { N... };
Run Code Online (Sandbox Code Playgroud)

c++ templates variadic-templates c++11

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

为什么这不能作为成员函数中的默认参数传递?

我试图将当前长度值作为默认参数作为函数参数传递.但编译器显示错误

"'这个'可能不会在这种情况下使用"

任何人都可以告诉我我犯的错误是什么.?

class A
{

    private:
    int length;
    public:
    A();
    void display(int l=this->length)
    {
        cout<<"the length is "<<l<<endl;
    }

};


int main()
{

    A a;
    a.display();    
    return 0;

}
Run Code Online (Sandbox Code Playgroud)

c++

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

包装Boost.Fusion序列

我正在寻找一种方法来创建一个Boost.Fusion序列包装器,它本身就是一个Fusion序列,并将所有'调用'转发到它的包装序列.有些东西

template< typename Sequence >
struct sequence_wrapper
{
    explicit sequence_wrapper( Sequence const& s ) : seq( s ){}

    Sequence seq;
};
Run Code Online (Sandbox Code Playgroud)

这里sequence_wrapper< Sequence >是一个融合序列以及和作品一样Sequence会.我需要这个的原因是我有几个函数在Fusion序列上运行(所有元素都满足一些特殊要求),我想添加一些语法糖,我需要一个自定义类型来添加重载运算符.我不需要sequence_wrapper上的操作结果来返回sequence_wrapper,只有语法糖相关的调用才会返回(手动)包装的序列.例如,使用逗号运算符将元素附加到序列(有点像Boost.Assign for Fusion序列):

template< typename Sequence, typename T >
sequence_wrapper<
    typename boost::fusion::result_of::push_back<
        Sequence const&
      , T
    >::type
> operator ,( Sequence const& seq, T const& v )
{
    return
        sequence_wrapper<
            typename boost::fusion::result_of::push_back<
                Sequence const&
              , T
            >::type
        >( boost::fusion::push_back( seq, v ) ) …
Run Code Online (Sandbox Code Playgroud)

c++ boost-fusion

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

模板别名和从属名称

在思考如何在C++ 11中改进CRTP时,我结束了以下代码:

template <typename Derived, typename Delayer>
struct derived_value_type
{
  typedef typename Derived::value_type type;
};

template <typename Derived>
struct base
{
  template <typename Delayer = void>
  typename derived_value_type<Derived, Delayer>::type
  foo(){ return {}; }
};

struct derived : base<derived>
{
  typedef int value_type;
};

#include <iostream>
#include <typeinfo>

int main()
{
  derived d;
  auto bar = d.foo();

  std::cout << typeid(bar).name() << ':' << bar << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

我相信以前的代码符合标准,并且它编译并与主要编译器一起工作(导致i:0).但是,当我使用模板别名时,由于derived不完整而导致编译错误:

template <typename Derived, typename Delayer> …
Run Code Online (Sandbox Code Playgroud)

c++ templates language-lawyer c++11

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

C++继承类的模板构造函数

根据我对C++继承的理解,无论何时调用子类的构造函数,都会自动调用父类的构造函数.对于模板化构造函数,模板参数的数据类型会自动进行,即我们不需要单独指定模板参数.该程序生成一个我似乎不理解的编译错误.

#include <iostream>
#include <list>
#include <algorithm>

using namespace std;

class A{
  public:
    int x;
    int y;
    int first(){
      return x;
    }
    int second(){
      return y;
    }
};

class C{
  public:
    float a,b;
    C(){
      a = 0.0f;
      b = 0.0f;
    }
    template<class T>
      C(T t){
        a = t.first();
        b = t.second();
      }
};

class D: public C{
  public:
    float area(){
      return a*b; 
    }
}

int main(){
  A a;
  a.x = 6;
  a.y = 8;
  C c(a);
  D d(a);
  cout<<c.a<<" "<<c.b<<" …
Run Code Online (Sandbox Code Playgroud)

c++ inheritance templates constructor

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

主要内部的功能声明和C中的外部主要功能

第一个程序编译得正确.第二个程序给出了错误,说foo的参数太少......编译器在两个程序中都忽略了全局声明?

第一个程序:

#include<stdio.h>
    void foo();
int main()
{
  void foo(int);
  foo(1);
  return 0;
}

void foo(int i)
{
   printf("2 ");
}

void f()
{
   foo(1);
}
Run Code Online (Sandbox Code Playgroud)

第二个程序:

void foo();
int main()
{
  void foo(int);
  foo();
  return 0;
}

void foo()
{
   printf("2 ");
}

void f()
{
   foo();
}
Run Code Online (Sandbox Code Playgroud)

c

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

与shared_ptr一起使用g ++ std :: bind错误

我无法理解为什么以下代码无法编译.

#include <memory>
#include <functional>

class Foo 
{
public:
  void Bar(int i) {}
};

void X(std::function<void(std::shared_ptr<Foo>)> f)
{

}

int main()
{
  std::shared_ptr<Foo> f(new Foo);
  auto f1(std::bind(&Foo::Bar, std::placeholders::_1, 1));
  X(f1);
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

g ++(4.6.3)输出......

n file included from /usr/include/c++/4.6/memory:80:0,
                 from test.cpp:1:
/usr/include/c++/4.6/functional: In static member function ‘static void std::_Function_handler<void(_ArgTypes ...), _Functor>::_M_invoke(const std::_Any_data&, _ArgTypes ...) [with _Functor = std::_Bind<std::_Mem_fn<void (Foo::*)(int)>(std::_Placeholder<1>, int)>, _ArgTypes = {std::shared_ptr<Foo>}]’:
/usr/include/c++/4.6/functional:2148:6:   instantiated from ‘std::function<_Res(_ArgTypes ...)>::function(_Functor, typename std::enable_if<(! std::is_integral<_Functor>::value), std::function<_Res(_ArgTypes ...)>::_Useless>::type) [with _Functor = std::_Bind<std::_Mem_fn<void (Foo::*)(int)>(std::_Placeholder<1>, int)>, …
Run Code Online (Sandbox Code Playgroud)

c++ bind g++ shared-ptr c++11

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

在Java中将毫秒转换为TDateTime

我在Android项目中需要保存TDateTime类型为(Delphi)的文件.我的日期是毫秒,但我不知道如何将毫秒转换为TDateTime.

我有这样的事情:

Date dateInMillis = new Date(System.currentTimeMillis());
double dateInDouble = ???;
Run Code Online (Sandbox Code Playgroud)

我会很高兴有任何提示可以帮助我解决这个问题.

java delphi android

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

拆分std :: index_sequence时出错

我试图分index_sequence成两半.为此,我index_sequence使用下半部分生成一个并使用它来跳过完整的前导元素index_sequence.以下是一个最小的测试用例,代表了我正在努力实现的目标:

template <int ...I>
struct index_sequence {};

template <int ...I, int ...J>
void foo(index_sequence<I...>, index_sequence<I..., J...>)
{}

int main()
{
    foo(index_sequence<0>{}, index_sequence<0, 1>{});
}
Run Code Online (Sandbox Code Playgroud)

我用最新版本的Clang,GCCMSVC试过这个,但它们都没有推断出来J....标准是否允许这样做?如果没有,为什么以及什么是实现我的意图的好方法?

c++ templates std c++11

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