c-preprocessor

Limiting Scope of #include Directives

吃可爱长大的小学妹 提交于 2019-12-07 20:50:33
问题 Let's say I have a header file with a class that uses std::string . #include <string> class Foo { std::string Bar; public: // ... } The user of this header file might not want std::string to be included in his/her project. So, how do I limit the inclusion to just the header file? 回答1: The user of your class must include <string> , otherwise their compiler will not know how big a Foo object is (and if Foo 's constructors/destructors are defined inline, then the compiler also won't know what

C++ using C code using double underscores in defines and identifiers

▼魔方 西西 提交于 2019-12-07 18:18:30
I understand that in C++ double underscores in identifiers are reserved for the compiler. I have some C code which has characteristics similar to this in the corresponding header files: extern "C" { #define HELLO__THERE 1 int hello__out__there( int ); } I will be using this header in a C++ project, and plan to be doing things in C++ like: if (HELLO__THERE == abc) hello__out__there(foo); Is this acceptable behavior in C++, covered by the standard? double underlines in identifiers are reserved for the compiler First, it's underscore I guess. Second such identifiers are reserved. That doesn't

C++ method declaration including a macro

只愿长相守 提交于 2019-12-07 16:55:26
I'm using QuickFAST library and while checking it I found this class declaration which I don't seem to really get ! I mean what does a macro name before the class name ! class QuickFAST_Export Message : public FieldSet also I found this declaration friend void QuickFAST_Export intrusive_ptr_add_ref(const Field * ptr); and again I don't get the use of this declaration ! for more info here's the QuickFAST_Export.hpp #ifdef _MSC_VER # pragma once #endif #ifndef QUICKFAST_EXPORT_H #define QUICKFAST_EXPORT_H // Compile time controls for library generation. Define with /D or #define // To produce or

Remove the comments generated by cpp

做~自己de王妃 提交于 2019-12-07 10:23:27
问题 I use #include ".../frontend/tokens.mll" in lexer.mll , and then cpp -C -P frontend/lexer.mll -o frontend/lexer_new.mll to generate lexer_new.mll . That worked until I upgraded my ubuntu from 12.04 to 14.04 yesterday. The compilation gives an error: ocamllex frontend/lexer_new.mll File "frontend/lexer_new.mll", line 1, character 1: illegal character /. make: *** [frontend/lexer_new.ml] Error 3 That is because in lexer_new.mll several lines of C comments have been inserted in the beginning: /*

Can a GHCI config file use CPP macros?

旧时模样 提交于 2019-12-07 10:13:45
问题 I was thinking it would be nice to set up my global GHCI config such that my commonly-used imports occur automatically when the packages that provide them are present. I tried adding this to ~/.ghc/ghci.conf : :set -XCPP #ifdef MIN_VERSION_containers import Data.Set (Set) import qualified Data.Set as Set import Data.Map (Map) import qualified Data.Map as Map #endif But apparently that does not work. > stack repl Configuring GHCi with the following packages: GHCi, version 8.0.2: http://www

How to detect C++11 under MSVC for noexcept feature?

柔情痞子 提交于 2019-12-07 10:10:37
问题 I'm working with a C++ library. The library's minimum requirements are C++03. I'm catching some warnings under Visual Studio 2015 regarding throwing destructors: ... algparam.h(271): warning C4297: 'AlgorithmParametersBase::~AlgorithmParametersBase': function assumed not to throw an exception but does ... algparam.h(271): note: destructor or deallocator has a (possibly implicit) non-throwing exception specification The throw was by design in the 1990s when the code was written, but its

Can the pre-processor directives like #include be placed only at the top of the program code?

最后都变了- 提交于 2019-12-07 09:46:12
问题 I have used the #pragma directive inside functions without error or warning(especially #pragma pack() ).But the following code shows the warning incompatible implicit declaration of built-in function 'printf'| : int main(void) { printf("Trial"); } #include<stdio.h> Further, here's is an extract from a book I have.The author has bad reviews on SO,especially for his generous use of void main() ,but still I feel no author can be that bad to claim the following without reason: Each of these

Unqualified-id before string constant

江枫思渺然 提交于 2019-12-07 07:22:26
On compiling following code I get error "expected unqualified-id before string constant" In file "Notification_Constants.h" namespace NOTIFICATION_CONSTANTS { #define SERVICE_EMAIL "service@company.com" } In file SendEmail.cpp #include "Notification_Constants.h" void UserPreferences::get_senders_email(String &_email) { _email = NOTIFICATION_CONSTANTS::SERVICE_EMAIL; } If i assign like following it works properly, what is the reason for the compilation error. _email = SERVICE_EMAIL; There is a similar question but the reason is not mentioned. String class declaration with relevant methods class

g++ -E option output

大兔子大兔子 提交于 2019-12-07 06:35:15
问题 Using this option I receive files after preprocessing. There are many lines like: # 91 "/usr/include/stdint.h" 3 4 What do the numbers mean? First I thought that #91 is the number of line where file is included, but that is not it. And about 3 4 I have no idea at all. 回答1: According to the official documentation, the line is of the format: # linenum filename flags The linenum specifies that the following line originated in filename at that line number. Then there are four flags: 1 - Start of

Where are macros stored?

人盡茶涼 提交于 2019-12-07 05:53:51
问题 If I use macros in my C code, such as #define var 10 then where exactly are the stored in the space allocated to the process by the kernel? In heap or BSS or global data? Or is it just a text replacement for var in one of the compiler passes? 回答1: Yes. the last one just a text replacement It is performed by a preprocessing pass. Some good details can be found here 回答2: Preprocessor directives like #define are replaced with the corresponding text during the preprocessing phase of compilation,