Can I declare ObjC block with auto?
auto fun = ^(int x) { NSLog(#"%d", x); }
fun(5);
I cannot work out valid syntax for that.
You are missing a ; after the declaration of fun. Otherwise, you got the syntax right, and Clang will accept that in -std=c++11 -fblocks mode, for C++ or Objective-C++ input. (Note that blocks are actually an orthogonal extension which is not part of Objective-C.)
I don't think the auto keyword from C++/Objective-C++ is used in objective-C.
As for declaring block variable for your example the following will work in objective-C
void(^fun)(int x) = ^(int x) {
NSLog(#"%d",x);
};
fun(5);
For more declaration options on block there's a very good answer here
The auto keyword is a c++11 keyword. Objective-c is a superset of c not c++ and therefor does not contain the properties of c++, but rather c. As for objective-c++, I do not believe that clang is up to date on all of the new c++11 features, especially in the compiler that builds objective-c++. Hope this helps!
Related
I tried to google and quick search on latest draft for "C lang" and "C18" on openstd org.
Will C++ standard support the latest standards of C?
C++ is a general purpose programming language based on the C programming language as described in ISO/IEC 9899:2018 Programming languages — C (hereinafter referred to as the C standard).
C++ provides many facilities beyond those provided by C, including additional data types, classes, templates, exceptions, namespaces, operator overloading, function name overloading, references, free store management operators, and additional library facilities.
http://eel.is/c++draft/intro.scope
C18 (previously known as C17) is the informal name for ISO/IEC 9899:2018, the most recent standard for the C programming language, published in June 2018. It replaced C11 (standard ISO/IEC 9899:2011).
https://en.m.wikipedia.org/wiki/C18_(C_standard_revision)
C++ (of any version) does not include C (of any version) wholesale. It merely references parts of the C specification as needed. For example, C++ includes (most) of the C standard library, and it does so by referencing the appropriate parts of the C standard instead of copying from it.
When C++20 references a version of the C specification, it references C18.
I believe the reasoning behind your question is that, when you use extern "C" in C++, it somehow invokes a separate C compiler of a specific version.
It doesn't. What extern "C" does is tell the C++ compiler to use C linkage for the functions so that other code using C linkage can properly link to these functions. It doesn't affect how the source code is compiled, apart from throwing compiler errors if you try to overload non-member functions inside the extern block.
The C++ compiler will not complain a bit if you write something like this:
extern "C" {
// this is still a C++ compiler, works as usual
class CPP
{
public:
// these are inside a class and can be overloaded,
// and they will be mangled as usual
static int foo(int i) { return i; };
static int foo(int i, int j) { return i + j; }
};
// these will not be mangled due to 'extern "C"'
int foo(int i) { return CPP::foo(i); }
int bar(int i, int j) { return CPP::foo(i, j); }
}
At the same time this simple C code will fail in any C++ compiler:
int * x = malloc(1);
As will this code from C11, since _Atomic is not a valid qualifier in the C++ standard:
#include <stdatomic.h>
_Atomic int x;
I know when i want to link C code as C code in C++ should i use extern "C". But with the following code :
/* file.h */
some (void)
{
return 10;
}
extern "C"
{
#include "file.h"
}
#include <iostream>
int main (void)
{
std::cout << some() << std::endl;
}
I get this compile time error:
C4430: missing type specifier - int assumed. Note: C++ does not support defualt-int.
How i can deal with this ?
I use MSVC2017 on MS-Windows10.
EDIT: I know that most declare the function with a explicit return type, But i what to use USBPcap and USBPcap declare some function like that. How i can use it in my own C++ program ?
All functions should specify a return type. You aren't specifying one for some.
In older versions of C, if you omit the return type of a function it defaults to int. C++ however doesn't support that.
You should always specify a function's return type:
int some(void)
{
return 10;
}
extern "C" only changes the linkage of declarations. In particular, it disables C++ name mangling that is otherwise needed for some C++ features such as overloading.
extern "C" does not make the enclosed part of the program to be compiled as C. As such, the declarations must still be well-formed C++. some (void) is not a well-formed declaration in C++, which explains the error.
How i can deal with this ?
Declare the return type explicitly.
USBPcap declare some function like that. How i can use it in my own C++ program ?
Then you cannot use that header. You can write a C++ compatible header yourself. Or, you can use a C++ compiler that supports impilict int as an extension.
P. S. Implicit int is not well-formed in C language either since C99.
Do not put any code to the .h files except static inline functions
Secondly declare the functions correctly - not the lazy way. What is some(void)? If the C++ compiler knew the return type of the function ....
extern "C"
{
int some (void)
{
return 10;
}
}
#include <iostream>
int main (void)
{
std::cout << some() << std::endl;
}
https://godbolt.org/z/_AJgFX
The compiler have say to you exactly what is the problem. The function that you are trying to call from std::cout need to have a returned type explicitly defined. In C the compiler automatically set the return type of an undefined-type function to int and you can compile a code that contain untyped function with nothing more of a warning from the compiler , but not do it, it is extremely wrong way to write code. In C++, just like the compiler told you, you can't, so it is an error. The definitions in C and C++ must have a type, if you want a function that not return anything, you need define it void.
However, in the last versions of the c++ standards you can use the auto keyword to auto detect the type of a declaration at compile time, so if you want that the compiler auto detect the type of something, use it, but use it with sparingly.
So, finally, if you want write C or C++ code, please, add a type to your definitions, this isn't python or javascript...
I know that meaning of auto keyword has been changed completely from C++11. But Recently I wrote a following simple program that compiles & runs fine when compiling with =std=c++98 option.
#include <iostream>
void fun(auto int a)
{
a=3;
std::cout<<a<<'\n';
}
int main()
{
fun(3);
}
Orwell Dev C++ IDE gives me warning like following:
[Warning] 'auto' changes meaning in C++11; please remove it [-Wc++0x-compat]
So, is it fine to use auto for function parameters or should I never use auto like this as in above program to maintain compatibility with C++11?
Until C++ 11 the auto keyword was a "storage class specifier" whereas with C++ 11 it becomes a type-induction specifier.
To answer your question: depending on the C++ standard you use to compile your code, adjust the use of the auto keyword accordingly. It's not portable across the pre/post C++ 11 boundary of the C++ standard.
So, is it fine to use auto for function parameters or should I never use auto like this as in above program to maintain compatibility with C++11?
That depends on what you mean by "fine":
If you mean "will it compile?" then YES.
If you mean "is it a good practice", the answer is that it is not a practice at all; It was possible to do so and the code was perfectly valid (before C++11) but that time is passed, and I do not know of anyone who did this (not even for tricky interview questions).
In conclusion, don't do it.
auto prior to C++11 was a storage class specifier, like register or static or extern.
It, however, was the default storage class specifier. Valid C++03 code with it removed would have the same meaning, which is why C++11 felt comfortable stealing the keyword.
In short:
void fun1(auto int a) {
std::cout<<a<<'\n';
}
void fun2(int a) {
std::cout<<a<<'\n';
}
have the same meaning in C++03. In C++11, fun1 is ill-formed.
Simply remove it from all of your pre-C++11 codebases. If the code was valid C++03, it will continue to have the same meaning.
There is a (very small) problem that some compilers might implement the K&R era C "by default, a type is int". Ie, they might consider auto x; to mean auto int x;. This was not valid C++03, however. Compiling with sufficiently strict flags in C++03 mode should generate errors around that (ab)use of auto.
As an aside, C++11 also introduces a new storage class specifier, thread_local. It steals auto for the use of auto-typed variables, and auto is no longer a storage class specifier.
In C++03 or earlier, is there a way of implementing the auto keyword? Not an object class, but so that it can be used like this [C++11]
auto x = 5;
std::cout << x;
I quickly 'whipped up' an implementation, but it is pretty rubbish, as you can cast it to any type - too much like an object class, and pretty basic, I know, but anyway, here it is:
class auto_t
{
public:
template < typename _Ty > auto_t(const _Ty &_Value)
: __data(_Value)
{
}
template < typename _Ty > operator _Ty()
{
return (_Ty)__data;
}
private:
void *__data;
};
#define auto auto_t
Not really. That's why C++11 introduces it as a keyword, and not a library feature.
There is no way to emulate the C++11 auto functionality with the same syntax. The code you supplied introduces a new type that just attempts to wrap your original type (ignoring the fact that it doesn't actually retain the the original object's lifetime). However, this new type will not follow the same rules for argument dependent lookup and type conversions as your original type. The auto keyword in C++11 is a language feature not a library extension. The best you could do to emulate it is with a MACRO like the boost library does.
I cannot think of any way doing what you want other than writing a compiler that inspects the intended type and creates the appropriate code for you.
That is a language feature and hence implemented by compilers.
Is return type deduction allowed for member functions in c++14, or only for free functions?
I ask because I sort of implicitly assumed it would work, but in gcc 4.8.1 I get an internal compiler error("in gen_type_die_with_usage"). First time I have ever gotten such a cryptic error like that, so I am a bit skeptical; and I know they have changed the spec since then.
For clarity this works for me:
auto foo() {return 5;}
but this doesn't:
class Bar{
auto baz() {return 5;}
}
Is this allowed in the draft standard?
Yes the standard should allow it according to the paper n3582. Here is an example from the paper.
Allowing non-defining function declarations with auto return type is not strictly necessary, but it is useful for coding styles that prefer to define member functions outside the class:
struct A {
auto f(); // forward declaration
};
auto A::f() { return 42; }
and if we allow it in that situation, it should be valid in other situations as well. Allowing it is also the more orthogonal choice; in general, I believe that if combining two features can work, it should work.
According to the comment by #bamboon, "Return type deduction is only supported as of gcc 4.9." so that would explain why you don't have it.