我能以某种方式拥有静态C宏吗?

时间:2016-02-17 08:44:27

标签: c macros c-preprocessor

我想根据另一个宏的值为宏指定一个值。但是,这两个宏是连接的。有办法避免这种情况吗?以下最小示例显示了我需要的内容:

#define VALUEA "A"
#define VALUEB "B"

// somewhere I define CHOOSE 
#define CHOOSE VALUEA

// MY_MACRO shall have the value of CHOOSE, but AT THE MOMENT of this line.
// I want to reassign CHOOSE while MY_MACRO stays the same.

#define MY_MACRO CHOOSE 
// now MY_MACRO has evaluates to CHOOSE evaluates to "A"

#define CHOOSE VALUEB
// now MY_MACRO has evaluates to "B" but I want it to be "A"

2 个答案:

答案 0 :(得分:1)

编译器会在编译前对整个项目进行预处理,所以如果你这样做会导致重新定义宏警告,CHOOSE总是最新定义的。

您应该将代码分为两种模式:

#define VALUEA "A"
#define VALUEB "B"

#ifndef CASE_B
    #define CHOOSE VALUEA
#endif

#ifdef CASE_B
    #define CHOOSE VALUEB
#endif

#ifdef CASE_A
    #define MY_MACRO CHOOSE 
#endif

因此您只需根据自己的目的定义CASE_ACASE_B

答案 1 :(得分:1)

一个简单的方法:在一个临时宏中“保存”宏的“值”:

@extends('app')

@section('content')
<div class="container-fluid">
    <div class="row">
        <div class="col-md-8 col-md-offset-2">
            <div class="panel panel-default">
                <div class="panel-heading">Login</div>
                <div class="panel-body">
                    @if (count($errors) > 0)
                        <div class="alert alert-danger">
                            <strong>Whoops!</strong> There were some problems with your input.<br><br>
                            <ul>
                                @foreach ($errors->all() as $error)
                                    <li>{{ $error }}</li>
                                @endforeach
                            </ul>
                        </div>
                    @endif

                    <form class="form-horizontal" role="form" method="POST" action="{{ url('/auth/login') }}">
                        {!! csrf_field() !!}

                        <div class="form-group">
                            <label class="col-md-4 control-label">E-Mail Address</label>
                            <div class="col-md-6">
                                <input type="email" class="form-control" name="email" value="{{ old('email') }}">
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-md-4 control-label">Password</label>
                            <div class="col-md-6">
                                <input type="password" class="form-control" name="password">
                            </div>
                        </div>

                        <div class="form-group">
                            <div class="col-md-6 col-md-offset-4">
                                <div class="checkbox">
                                    <label>
                                        <input type="checkbox" name="remember"> Remember Me
                                    </label>
                                </div>
                            </div>
                        </div>

                        <div class="form-group">
                            <div class="col-md-6 col-md-offset-4">
                                <button type="submit" class="btn btn-primary">Login</button>

                                <a class="btn btn-link" href="{{ url('/password/email') }}">Forgot Your Password?</a>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection