我曾尝试使用此建议来执行静态断言,但如果我在模板的方法中使用它,则不会出现编译错误.
示例如下:
#include <iostream>
#define STATIC_ASSERT(expr, msg)               \
{                                              \
    char STATIC_ASSERTION__##msg[(expr)?1:-1]; \
    (void)STATIC_ASSERTION__##msg[0];          \
}
template <typename T >
class A
{
public:
  int foo(const int k )
  {
    // does not work
    STATIC_ASSERT( k > 9, error_msg );
    return k+5;
  }
};
int bar(const int k )
{
  // works fine
  //STATIC_ASSERT( k > 9, error_msg );
  return k+5;
}
int main()
{
  A<int> a;
  const int v = 2;
  std::cout<<a.foo(v)<<std::endl;
  std::cout<<bar(v)<<std::endl;
  // works fine
  //STATIC_ASSERT( …所以我一直在尝试获取数组大小及其元素的输入,然后将元素显示到屏幕上,但是当我举例来说数组的大小:7数组的元素:1 2 3 4 5 6 7输出是:
1
2
3
4
5
6
6
代码 :
#include <iostream>
using namespace std;
int main () {    
    int n , Arr[n];    
    cout << "please put the size of the array " ;    
    cin >> n;    
    cout << "please enter array's elemets ";    
    for (int k=0; k<n ; k++) {    
        cin >> Arr[k];    
    }    
    for (int i=0;i<n;i++){    
        cout << Arr[i] << endl;    
    }    
}
基于额外的括号,下面两个 return 语句之间的表达式求值有什么区别吗?
    return a++ *(-b+123.456)/999.12344;
与
    return (a++ *(-b+123.456)/999.12344);
编程语言 C++ 标准 CPP 98'ish(C++11 之前)
希望问题清楚。期望是对表达式进行完整评估。
有没有办法char[]通过char*C++ 中的指针来确定缓冲区的大小?我正在使用 C++98。
以下代码行都打印出“8”(本质上是sizeof(char*))。但是我正在寻找缓冲区的实际大小。
int maxBufferLen = 24;
char* buffer = new char[maxBufferLen];
std::cout << sizeof(buffer) << std::endl; // prints '8'
std::cout << ( sizeof(buffer) / sizeof(buffer[0]) ) << std::endl; // prints '8'
在实践中,我char*在函数之间传递该缓冲区,我将无法访问maxBufferLen我用来初始化它的变量。因此,我需要使用另一种方式来确定长度。
在代码库中,我有许多部分根据启用的功能打开或关闭。目的是生成尽可能小的程序代码(Arduino 具有 32kB 程序存储器)。
可以说我的代码如下:
class A
{
private:
#ifdef FEATURE
    int m_optA;
    int m_optB;
#endif
public:
#ifdef FEATURE
    void SetFeatureOptions(int optionA, int optionB)
    {
        m_optA = optionA;
        m_optB = optionB;
    }
#endif
};
#ifdef FEATURE
#define SETFEATUREOPTIONS(a, b) SetFeatureOptions(a, b)
#else
#define SETFEATUREOPTIONS(a, b) Noop() // ????? <-- what should I put here to perform NOOP
#endif
class B
{
public:
    A m_a;
    void DoStuff()
    {
        // approach 1:
#ifdef FEATURE
        m_a.SetFeatureOptions(1, 34);
#endif
        // approach 2:
        // Lets …我试图使用以这种方式定义的结构:
在文件编号1中定义了结构(我无法修改此文件):
struct prueba
    {
        prueba(int const & a_):a(a_)
        {};
        int a;
    };
然后在头文件中,struct被声明为类的变量:
prueba st_prueba;
然后在源文件中,结构的构造函数在类的成员函数中调用:
st_prueba(3);
我收到了下一个错误:
错误:没有匹配函数来调用'main():: prueba :: prueba()'
我在其他问题中看到了这个问题的答案:
他们说你必须添加默认构造函数.但是在我的情况下,我无法控制结构的定义.
这个想法是有两个不同的步骤,以便将声明放在一个文件中,并在另一个文件中调用构造函数.
我该怎么做才能使用这段代码?
我需要在c ++ 98中重写c ++ 11代码,c ++ 11正在使用这个lambda函数
std::for_each(m_outputs.begin(), m_outputs.end(), [&](const Output & o)
{
     process(o)
}
使用仿函数是否容易编写完全相似的东西?