Preprocessor

Dec 13, 2013 at 8:00pm
Hello.
I made an framework (Boost-based), but I have the following problem: if an variable is something do something, if is another something do another thing. But else, I need to give an error to the compiler. How can I do this?
I think that you won't need to see this, but if you want...

basic_io.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#ifndef BASIC_IO_H
#define BASIC_IO_H
#include <ostream>
#include <iostream>
#include <string>
#include <sstream>
#include <thread>
#include <chrono>
void wait_input(void)
{
    std::cin.ignore();
}
void pause(const char* lang)
{
    if(lang == "en")
    {
        std::cout<<"Press any key to continue...";
        wait_input();
    }
    if(lang == "pt")
    {
        std::cout<<"Aperte qualquer tecla para continuar...";
    }
    else()
    
}
#endif // BASIC_IO_H


Thanks!
Dec 13, 2013 at 8:41pm
You can make your function create array of size 0. Arrays of size 0 are not allowed and would cause compiler error, but wait a second, and I will tell you some other ideas.

Okay:
http://f3.tiera.ru/other/DVD-009/Alexandrescu_A._Modern_C++_Design%5Bc%5D_Generic_Programming_and_Design_Patterns_Applied_%282001%29%28en%29%28271s%29.pdf

found in internet.

You can check site 33. The subchapter describes the problem.
Last edited on Dec 13, 2013 at 8:45pm
Dec 13, 2013 at 8:44pm
lang has no value at compile-time, so how would the compiler be able to check it and issue in error if it is an incorrect value?

One simple strategy is to use #defines and a preprocessor check that conditionally uses #error. I'm almost sure there are some C++11 features that can help you here if you put the value of lang in a constexpr variable somewhere.
Dec 13, 2013 at 9:09pm
closed account (S6k9GNh0)
Some compilers support an array of size zero as an extension (non-standard). I'm not sure if the MVC++ allows it though... http://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html
Dec 13, 2013 at 9:18pm
Wouldn't it require a special flag to be attached? Anyway, since it's non-standard, many things can not be allowed... But the thing described in the book I attached will always work :)
Dec 14, 2013 at 1:18am
closed account (S6k9GNh0)
Anyways, if you mean that you need it based on types, you should look into template specialization and metaprogramming. If you're just wanting language-based decisions, you need to look into locale.
Dec 14, 2013 at 1:29am
Can you give me any example?
Dec 14, 2013 at 10:29am
If you want to have the compiler get an error, you could not execute the program. When you compile a program the compiler looks for syntax mistakes o.s. and if he finds someones, he aborts the compilation so that you dont gain an .exe file. Furthermore, during runtime, the compiler does nothing. So, if you want to have the program abort when you enter e.g. "senseless" for your language setting, you could write the following:

else {
exit(0);
}

or, if youre in WINAPI,

PostQuitMessage(0);
Dec 14, 2013 at 11:38am
@Jetkeynature I mean: if he makes an inavaible cho I ice, the compiler don't let the program be compiled. I don't want to the prgram be buggy.
Dec 14, 2013 at 4:47pm
So you mean that the compiler should ask the user during programming ? Thats not rly possible. You would have to edit your Developing Tool's Source code.
Dec 14, 2013 at 6:18pm
closed account (S6k9GNh0)
jetkeynature, that is wrong. You can use static_if statements as well as some other metaprogramming techniques. You need to do the research yourself though, most C++ programmers simply don't know much about it because it's a complicated matter.
Dec 14, 2013 at 6:26pm
IQChange - write a chapter of a book that I showed to you - it has a ready solution that will work... hopefully :P(because Alexandrescu's programs require latest C++ functionality, not every compiler has it; try to use it, and hopefully you will get what you want).
Topic archived. No new replies allowed.