【问题标题】:Imitating fortran print and write syntaxes in C++在 C++ 中模仿 fortran 打印和写入语法
【发布时间】:2015-06-20 03:31:53
【问题描述】:

我正在尝试在 C++ 中实现一个类来模仿 FORTRAN 中的 print 和 write 语句的语法。

为了实现这一点,我实现了一个类fooprint 并重载了fooprint::operator,(逗号运算符)。由于这个类应该打印到标准输出或文件,我还定义了两个宏:print(用于标准输出)和write(用于操作文件)。

我在尝试使用 write(data) a; 时遇到编译错误(请参阅下面的错误日志)。 如何获得具有上述属性的有效write 语句?

这是代码(Live Demo):

#include <iostream>
#include <fstream>

class fooprint
{
    private:
      std::ostream *os;

    public:
      fooprint(std::ostream &out = std::cout) : os(&out) {}
      ~fooprint() { *os << std::endl;}

      template<class T>
      fooprint &operator, (const T output)
      {
        *os << output << ' ';
        return *this;
      }
};

#define print      fooprint(),     // last comma calls `fooprint::operator,`
#define write(out) fooprint(out),

int main()
{
  double a = 2.0;

  print "Hello", "World!";        // OK
  print "value of a =", a;        // OK
  print a;                        // OK

  std::ofstream data("tmp.txt");
  write(data) "writing to tmp";   // compiles with icpc; it doesn't with g++ 
  write(data) a;                  // this won't compile
  data.close();
  return 0;
}

以及编译信息:

g++ -Wall -std=c++11 -o print print.cc
   error: conflicting declaration ‘fooprint data’
   #define write(out) fooprint(out),
                                   ^
   note: in expansion of macro ‘write’
     write(data) a;
     ^
   error: ‘data’ has a previous declaration as ‘std::ofstream data’
   error: conflicting declaration ‘fooprint a’
     write(data) a;
                 ^
   error: ‘a’ has a previous declaration as ‘double a’

icpc -Wall -std=c++11 -o print print.cc
   error: "data" has already been declared in the current scope
     write(data) a;
   error: "a" has already been declared in the current scope
     write(data) a;

【问题讨论】:

  • out 周围再放一对大括号(即fooprint((out))fooprint{out}),这样它就构成了一个临时声明而不是变量声明。
  • :o 它与大括号一起使用:fooprint({out})。如果我不使用 c++11 怎么办?
  • 我的错误,fooprint((out)) 应该是 (fooprint(out))。这应该适用于任何版本的 C++。
  • 完美!谢谢。你从哪里学到的这个“技巧”? :o
  • 我是从几年前看到的评论中了解到的。现在你学会了! :)

标签: c++ fstream ostream


【解决方案1】:

fooprint(out) 不是创建临时变量,而是声明类型为 fooprint 的变量,其名称作为宏的参数提供。为了不将此作为声明,而是将其作为表达式,您可以进行两个快速更改,即用括号括起来 (fooprint(out)) 或使用大括号初始化 (C++11) (fooprint{out})。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-25
    • 2013-08-30
    • 1970-01-01
    • 2013-03-06
    • 1970-01-01
    • 2012-12-19
    • 2021-03-23
    相关资源
    最近更新 更多