Class template argument type deduction in C++17 - compilation problems - g++

I'm following Kate Gregory's C++ course on Pluralsight and understand that C++17 introduced a feature for compilers to deduce the type in a template, however the code below returns the error: missing template arguments before 'numbers'
#include <vector>
using std::vector;
int main()
{
vector numbers{ 0, 1, 2 };
return 0;
}
I'm using the MinGW gcc compiler (version 6.3.0) and using "g++ -std=c++1z *.cpp" in the command prompt, so I'm not sure what I'm doing wrong.
I know I can fix it by declaring the type but I wanted to check in case I miss out on other C++17 features through some basic error I'm making.

Your code is OK (but I suggest not to use using std::vector).
The problem is your compiler, g++ 6.3.0, that is too old to support the feature you're trying to use: class template argument deduction and deduction guides.
You need g++ 7 or newer.

Related

Porting project from ICCAVR to Atmel Studio 7.0 _flash problem

I'm currently porting a large project to Atmel Studio from ICCAVR as I need to use some of the ASF libraries for the project I'm working on.
I'm getting this error with the code example below in regards to trying to convert the following line to Atmel Studio
void debugoutf(__flash char * header, __flash char * msg);
Error pointer targeting address space '__flash' must be const in
function parameter 'header'
I tried using the macro in the documentation so that it can compile in Atmel Studio.
#ifndef FLASHVAR_H_
#define FLASHVAR_H_
#include <avr/pgmspace.h>
#if defined(__ICCAVR__) // IAR C Compiler
#define FLASH_DECLARE(x) __flash x
#endif
#if defined(__GNUC__) // GNU Compiler
#define FLASH_DECLARE(x) x __attribute__((__progmem__))
#endif
IAR to AVR Conversion
void debugout(FLASH_DECLARE (char * header), char * msg);
My question is have I done the conversion correctly, as I don't think I've done it correctly as nothing is getting printed out in my UART debugging.
The problem with using __attribute__((__progmem__)) on a variable is that whenever you want to read data from such a variable, you can't just access it in the usual way that you would access a variable from RAM. Instead, you have to use special functions provided by the avr/pgmspace.h header in avr-libc, like pgm_read_byte.
That's pretty annoying, but you don't have to do it that way because why they added named address spaces like __flash to recent versions of GCC (assuming you are using C, not C++). I recommend you stop using __attribute__((__progmem__)) and use __flash instead, if your version of GCC supports it. If the compiler gives you an error or warning because it expects items stored in flash to be marked as const, you can simply add const to the declaration/definition (in the right position). If you have trouble doing that, please post a new question or edit this question so it contains a MCVE.
For the example code you gave, you should try writing:
void debugoutf(const __flash char * header, const __flash char * msg);

cmake CheckSymbolExists for intrinsic

I'd like to check for intel intrinsics such as _mm_popcnt_u32 or _mm_blendv_epi8 using cmake. However the function check_symbol_exists doesn't work properly depending on the compiler. (it works with Clang but not with GCC). Indeed it is documented
If the header files declare the symbol as a function or variable then the symbol must also be available for linking (so intrinsics may not be detected).
Is there a simple way to check for those ?
As the CMake documentation also states:
If the symbol is a type, enum value, or intrinsic it will not be
recognized (consider using CheckTypeSize or CheckCSourceCompiles).
So, try to compile a small example with the Intel intrinsic using CheckCSourceCompiles. E.g.:
include(CheckCSourceCompiles)
check_c_source_compiles("
int main() {
int tmp = _mm_popcnt_u32(0);
return 0;
}
"
HAVE_INTRINISC
)

What compiler option/library do I need to use detect_or_t type trait?

I am trying to use std::experimental::detect_or_t from <experimental/type_traits>.
What compiler, option, version or library do I need to compile the following example from http://en.cppreference.com/w/cpp/experimental/is_detected ?
#include <experimental/type_traits>
#include <cstddef>
template<class T>
using diff_t = typename T::difference_type;
template <class Ptr>
using difference_type = std::experimental::detected_or_t<std::ptrdiff_t, diff_t, Ptr>;
struct Meow { using difference_type = int; };
struct Purr {};
int main()
{
static_assert(std::is_same<difference_type<Meow>, int>::value, "Meow's difference_type should be int!");
static_assert(std::is_same<difference_type<Purr>, std::ptrdiff_t>::value, "Purr's difference_type should be ptrdiff_t!");
}
I tried using clang++ -std=c++14 and g++ -std=c++14. Also with -std=c++1y and -std=c++17. I always get this:
main.cpp:8:44: error: 'detected_or_t' in namespace 'std::experimental' does not name a template type
Those traits were first added to libstdc++ in GCC 6.1.0, as documented in the GCC 6 release notes:
Experimental support for most features of the second version of the Library Fundamentals TS.
And the implementation status tables in the manual, at
https://gcc.gnu.org/onlinedocs/gcc-6.1.0/libstdc++/manual/manual/status.html#table.cxx1z_ts_status
I'm less sure about libc++, but they're not supported by the version in Clang 3.9.1 but are supported by the current trunk, so I think they first appeared in Clang 4.0.0

Strange "selector mangling" in Objective-C method with Boolean arguments

I need to recover method names dynamically, via reflection calls at runtime. But get strange results for some.
My TestClass contains a method like:
- (void)testMethod6_NSRect:(NSRect)a1 int:(int)a2 int:(int)a3 bool:(Boolean)a4 {
...
}
When enumerating the above classes methods using class_copyMethodList() and fetching the method selectors via method_getName(), I get:
"testMethod6_NSRect:int:int:_Bool:"
Thus, the selector was rewritten somehow (by gcc?) to make "_Bool" from "bool". As far as I tested yet, this seems to be done only for a "bool" selector-part - if I change it to int:(int), as in:
- (void)testMethod1_int:(int)a1 int:(int)a2 int:(int)a3 int:(int)a4 {
...
}
I get the expected:
"testMethod1_int:int:int:int:"
Q:
Does anyone know the rules or a pointer to where I could find them for this "selector rewriting", or am I missing something? Is this only done for "bool"?
I also need to know if this behavior is depending on the gcc-version, osx-version or runtime library version.
I am using (gcc --version):
i686-apple-darwin10-gcc-4.2.1 (GCC) 4.2.1 (Apple Inc. build 5666) (dot 3)
on a (uname -a)
10.8.0 Darwin Kernel Version 10.8.0:
The problem lies in an ugly piece of preprocessor magic in the C99 standard header <stdbool.h>:
#define bool _Bool
C99 defines a type named _Bool which behaves like C++'s bool type. The define is there to be able to use it in C but with the C++ identifier.
Solution:
#undef bool
Try using BOOL instead of Boolean

Non-static data member error with clang but not g++

Summary:
I have a code snippet that compiles fine with g++ but not with clang.
Details:
I have a project that compiles fine with g++ but when compiling with clang I get an error about error: use of non-static data member. I tried to create a small test case that would demonstrate the problem, but for the small test case g++ gave the same error as clang.
I've posted a 236 line file to pastebin that demonstrates the problem:
http://pastebin.com/DGnfxmYe
When compiled with g++ 4.6.3 this works fine. But when compiled with clang 3.2 I get the following error messages:
myhashmap.hpp:169:29: error: use of non-static data member 'num_bins' of 'MyHashMap' from nested type 'iterator'
for (_index++; (_index < num_bins) && (bins[_index] == NULL); _index++)
^~~~~~~~
myhashmap.hpp:169:43: error: use of non-static data member 'bins' of 'MyHashMap' from nested type 'iterator'
for (_index++; (_index < num_bins) && (bins[_index] == NULL); _index++)
^~~~
myhashmap.hpp:171:17: error: use of non-static data member 'num_bins' of 'MyHashMap' from nested type 'iterator'
if (_index < num_bins) {
^~~~~~~~
myhashmap.hpp:172:17: error: use of non-static data member 'bins' of 'MyHashMap' from nested type 'iterator'
_theNode = bins[_index];
^~~~
Looking at the code, it makes sense to me why clang is giving these error messages. What I don't understand is why g++ compiled the code correctly in the first place. I did not write this code but I would like to get it to compile cleanly with clang. So I'm trying to understand exactly what it is doing. And I would be interested in understanding why it compiles with g++ but not with clang. Does g++ interpret the c++ standard differently, or is there some g++ extension that the code is taking advantage of?
It fails with GCC 4.8 (prerelease) so I assume it's a bug that's been fixed. I can't find a corresponding bug report though.
To fix the code I think you'll need add an int _num_bins member to the iterator and pass the cotnainer's num_bins to the iterator constructor in begin() and end(), so it's stored in each iterator object.
(Additionally, don't write (void) for a function taking no arguments, that's an abomination. In C++ a function taking no arguments is written ())