使用#define在C ++ / g ++中包含文件

时间:2019-05-16 18:11:43

标签: c++ g++

我不想写:

''' <summary> Reference to the awaiting task. </summary>
''' <value> The awaiting task. </value>
Protected ReadOnly Property AwaitingTask As Threading.Tasks.Task

''' <summary> Reference to the Action task; this task status undergoes changes. </summary>
Protected ReadOnly Property ActionTask As Threading.Tasks.Task

''' <summary> Reference to the cancellation source. </summary>
Protected ReadOnly Property TaskCancellationSource As Threading.CancellationTokenSource

''' <summary> Starts the action task. </summary>
''' <param name="taskAction"> The action to stream the entities, which calls
'''                           <see cref="StreamEvents(Of T)(IEnumerable(Of T), IEnumerable(Of Date), Integer, String)"/>. </param>
''' <returns> The awaiting task. </returns>
Private Async Function AsyncAwaitTask(ByVal taskAction As Action) As Task
    Me._ActionTask = Task.Run(taskAction)
    Await Me.ActionTask '  Task.Run(streamEntitiesAction)
    Try
        Me.ActionTask?.Wait()
        Me.OnStreamTaskEnded(If(Me.ActionTask Is Nothing, TaskStatus.RanToCompletion, Me.ActionTask.Status))
    Catch ex As AggregateException
        Me.OnExceptionOccurred(ex)
    Finally
        Me.TaskCancellationSource.Dispose()
    End Try
End Function

''' <summary> Starts Streaming the events. </summary>
''' <exception cref="InvalidOperationException"> Thrown when the requested operation is invalid. </exception>
''' <param name="bucketKey">            The bucket key. </param>
''' <param name="timeout">              The timeout. </param>
''' <param name="streamEntitiesAction"> The action to stream the entities, which calls
'''                                     <see cref="StreamEvents(Of T)(IEnumerable(Of T), IEnumerable(Of Date), Integer, String)"/>. </param>
Public Overridable Sub StartStreamEvents(ByVal bucketKey As String, ByVal timeout As TimeSpan, ByVal streamEntitiesAction As Action)
    If Me.IsTaskActive Then
        Throw New InvalidOperationException($"Stream task is {Me.ActionTask.Status}")
    Else
        Me._TaskCancellationSource = New Threading.CancellationTokenSource
        Me.TaskCancellationSource.Token.Register(AddressOf Me.StreamTaskCanceled)
        Me.TaskCancellationSource.CancelAfter(timeout)
        ' the action class is created withing the Async/Await function
        Me._AwaitingTask = Me.AsyncAwaitTask(streamEntitiesAction)
    End If
End Sub

相反,我只想写:

#include MACRO(arg) 

很多人说我们不能写这样的宏来#include任何文件,但是我认为那些尝试只能在预处理阶段失败,我认为低于一个就可以通过预处理器,但是稍后会失败。

我编写了以下测试代码tmp1.cpp(无法编译):

MACRO(arg)

下面是一些命令的输出:

#define HASH #
#define ZX(arg) HASH include <arg>

ZX(iostream)

int main()
{

}

我想知道为什么tmp2.cpp可以编译并成功执行,但是tmp1.cpp在编译阶段失败了吗?是不是先运行预处理器,然后将其输出馈送到编译器? tmp2.cpp只是tmp1.cpp的预处理版本。

[编辑]

我有以下要求:

$ g++ -E tmp1.cpp
# 1 "tmp1.cpp"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "/usr/include/stdc-predef.h" 1 3 4
# 1 "<command-line>" 2
# 1 "tmp1.cpp"



 # include <iostream>

int main()
{

}
$ g++ -E tmp1.cpp > tmp2.cpp
$ g++ tmp2.cpp
$ ./a.out
$ g++ tmp1.cpp
tmp1.cpp:1:14: error: stray ‘#’ in program
 #define HASH #
              ^
tmp1.cpp:2:17: note: in expansion of macro ‘HASH’
 #define ZX(arg) HASH include <arg>
                 ^
tmp1.cpp:4:1: note: in expansion of macro ‘ZX’
 ZX(iostream)
 ^
tmp1.cpp:2:22: error: ‘include’ does not name a type
 #define ZX(arg) HASH include <arg>
                      ^
tmp1.cpp:4:1: note: in expansion of macro ‘ZX’
 ZX(iostream)
 ^
$ 

1 个答案:

答案 0 :(得分:8)

  

[cpp.rescan] / 3 结果得到的完全由宏替换的预处理令牌序列即使类似于一个,也不会作为预处理指令进行处理。

我相信您的追求是没有希望的。