Compiler error C3538 in Visual Studio using std::bind - c++11

I have problems to compile the following small code fragment. Visual Studio 2015 has problems in the deduction of the type of remover. Can someone explain me why and how to fix this error?
My idea was to create a reuseable function deleting the first occurence of a value in a given STL container under some user definable predicate.
#include <algorithm>
#include <iostream>
#include <iterator>
#include <functional>
#include <vector>
template<typename T>
auto removeOnlyOnce = [](std::vector<T>& v, const std::function<bool(const T&)>& pred) {
auto iter = std::find_if(v.begin(), v.end(), pred);
if (iter != v.end()) {
v.erase(iter);
}
};
int main(int argc, char** args) {
std::vector<int> v{ 1,2,3,4,2,2,3 };
// Works
std::function<bool(const int&)> isTwoPred = [](int x) { return x == 2; };
removeOnlyOnce<int>(v, isTwoPred);
// Also works
removeOnlyOnce<int>(v, [](const int x)->bool { return x == 2; });
// Gives compile error: C3538
// auto remover = std::bind(removeOnlyOnce<int>, v, std::placeholders::_1);
// remover([](const int x)->bool { return true; });
std::copy(v.begin(), v.end(), std::ostream_iterator<int>(std::cout, " "));
}
The compiler issues the following warning:
main.cpp(26): error C3538: In einer Deklaratorliste muss "auto" immer in denselben Typ hergeleitet werden.
main.cpp(26): note: kann "<lambda_d00b55a1f2cac59f0efd4a81f45edea3>" sein
main.cpp(26): note: oder "std::_Binder<std::_Unforced,<lambda_d00b55a1f2cac59f0efd4a81f45edea3> &,std::vector<int,std::allocator<_Ty>> &,const std::_Ph<1> &>"
with
[
_Ty=int
]
Compiling with Wandbox and Option C++11 gives the following warning, which might be important:
prog.cc:8:6: warning: variable templates are a C++14 extension [-Wc++14-extensions]
auto removeOnlyOnce = [](std::vector<T>& v, const std::function<bool(const T&)>& pred) {
^
1 warning generated.

This lambda is not local. Please omit the & in [&] and you should be fine. Maybe a little cryptic, what I wrote: In other words, you don't have a this to capture.

Related

C++ Internal compiler error working with native events

__event T e(args);
On line 9, VS gives me green squigglies under 'e' with the warning: Function definition for 'e' not found.
On compile/build, it throws the C1001 internal error occurred in the compiler (line 9 again).
I've tried renaming the variable, tried removing the template and just working with normal types, tried making it public.
If anyone can help it would be greatly appreciated, thank you.
(Whole code probably not necessary but just to give an idea what I'm going for)
#include <cstdarg>
#include <functional>
#include <iostream>
template <typename T, typename ... args>
[event_source(native)]
class Action {
private:
__event T e(args);
public:
~Action() {
__unhook(this);
};
void operator +=(std::function<T(args...)> f) {
__hook(e, this, &f);
}
void operator -=(std::function<T(args...)> f) {
__unhook(e, this, &f);
}
void operator()(args...) {
__raise e(args);
}
};
void print(const char* s) {
std::cout << s << std::endl;
}
int main() {
Action<void, const char*> printAction;
printAction += print;
printAction("Print a string.");
printAction -= print;
}

How to make msvc 14 evaluate my constexpr at compile time

I am experimenting with c++ constexpr. I am implementing a HashedString class using FNV-1a hash.
Everything seems fine except that visual studio 2015 update 3 doesn't seem to evaluate the constexpr at compile time.
I added a static_assert and it shows no error, but in my test's disassembly it is clear that there is an explicit call to the constexpr function instead of the precomputed value.
I also tried with g++ and clang and they both are able to evaluate the constexpr at compile time.
Here is my test code:
#include <cstdint>
#include <cstddef>
#include <string>
class HashedString {
public:
//value working only for a 32bit hash
constexpr static size_t defaultOffset = 2166136261u;
constexpr static size_t prime = 16777619u;
/**
* Compute the hash of a string at compile time using FNV-1a hash
* https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80 %93Vo_hash_function
*/
template<std::size_t N>
constexpr HashedString(const char(&a)[N]) noexcept
: mHash(hash(a))
#if defined(_DEBUG)
, mString(a)
#endif
{
}
explicit constexpr HashedString(size_t h) noexcept : mHash(h) {}
constexpr static size_t hash(const char *const aString, const uint32_t val = defaultOffset) noexcept
{
return (aString[0] == '\0') ? val : hash(&aString[1], (val ^ uint32_t(aString[0])) * prime);
}
constexpr bool operator==(const HashedString & hs) const { return mHash == hs.mHash; }
constexpr bool operator==(const size_t & h) const { return mHash == h; }
constexpr bool operator!=(const HashedString & hs) const { return mHash != hs.mHash; }
constexpr bool operator!=(const size_t & h) const { return mHash != h; }
constexpr bool operator<(const HashedString & hs) const { return mHash < hs.mHash; }
private:
const size_t mHash = 0;
#if defined(_DEBUG)
const char* mString = nullptr;
#endif
};
static_assert(HashedString("FNV Hash Test") == 0xF38B3DB9, "HashedString of 'FNV Hash Test' shoulb be equal to 0xF38B3DB9");
int main(int , char**) {
constexpr HashedString hs("FNV Hash Test");
return hs == 0xF38B3DB9;
}
So my question is: Is there a way to make visual studio compute my constexpr at compile time?
Changing main to:
constexpr auto hash = HashedString::hash("FNV Hash Test");
return hash == 0xF38B3DB9;
or
constexpr HashedString hs("FNV Hash Test");
constexpr auto answer = hs == 0xF38B3DB9;
return answer;
will cause the hash to be computed at compile time. The way your code was there was no demand on the compiler to compute the hash at compile-time. By requiring the compiler to initialize a constexpr variable, it is forced to compute the value at compile-time. Which reduced main's code to:
mov eax,1
ret
Booyah! for VS2015's SSA optimizations.

GCC produce "could not convert" error when using aggregate initialization

I'm trying to make C-string size calculation at compile time, using code like this:
#include <stdio.h>
#include <stdint.h>
class StringRef
{
public:
template<int N>
constexpr StringRef(const char (&str)[N])
: m_ptr(str), m_size(uint32_t(N-1)) {}
constexpr const char *constData() const
{ return m_ptr; }
private:
const char *m_ptr;
uint32_t m_size;
};
struct S
{
StringRef str;
};
constexpr static const struct S list[] =
{
"str",
};
int main()
{
printf("%s\n", list[0].str.constData());
return 0;
}
In clang-3.7 everything is fine, but in GCC 4.9.3-5.3 I get:
error: could not convert '(const char*)"str"' from 'const char*' to
'StringRef'
It can be fixed by adding explicit braces:
constexpr static const struct S list[] =
{{
{ "str" },
}};
But code became ugly and, still, clang somehow understand it correctly.
How can I make gcc understand array initialization without explicit braces?

Visual Studio 2010 assembler listing

I use VS2010 to generate an assembler listing. Than I create a new project, add generated *.asm file and try to compile it. During the compillation I receive some erros like:
main.asm(54): error A2008: syntax error : lambda0
at this line of generated asm:
PUBLIC ??R<lambda0>#?A0x1262112e##QBE_NH#Z ; `anonymous namespace'::<lambda0>::operator()
Another one is:
main.asm(72): error A2039: line too long
The length of 72th line is 538 symbols.
Looks like Visual Studio generates an incompatible asm code for examle for lambda expressions.
The next C++ code I used for test:
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
#include <cctype>
std::string LTrimString(std::string s)
{
s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](int ch) { return !std::isspace((unsigned char)ch); }));
return s;
}
std::string RTrimString(std::string s)
{
s.erase(std::find_if(s.rbegin(), s.rend(), [](int ch) { return !std::isspace((unsigned char)ch); }).base(), s.end());
return s;
}
std::string TrimString(std::string s)
{
return LTrimString(RTrimString(s));
}
bool StartsWith(std::string full_str, std::string start_str)
{
return full_str.substr(0, start_str.length()) == start_str;
}
int main()
{
std::ifstream ifs("test.txt");
while(ifs)
{
std::string line;
std::getline(ifs, line);
if(StartsWith(TrimString(line), ">>>"))
std::cout << "###";
std::cout << line << '\n';
}
}
The question is: does it possible to generate correct asm file for compilation in visual studio or it can not be used for compilation?

Clang OS X compiler balks on this code but it works fine on Linux

The following sample code works fine under linux using g++4.8.2, using boost1_56. However, I get a strange linker error under MacOS X (Yosemite) using clang:
ld: internal error: atom not found in symbolIndex(__ZNSt3__112__hash_tableINS_17__hash_value_typeIKNS_12basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEEEN15FRUIT_TUPLES4dataEEENS_22__unordered_map_hasherIS8_SB_NS9_8key_hashELb1EEENS_21__unordered_map_equalIS8_SB_NS9_9key_equalELb1EEENS5_ISB_EEE15__insert_uniqueIRKNS_4pairIS8_SA_EEEENSL_INS_15__hash_iteratorIPNS_11__hash_nodeISB_PvEEEEbEEOT_) for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Proces
The main.cpp file
#include "TupleFruits.hpp"
int main()
{
map_t fruitHash = InitializeFruitHash();
std::string fruit = "BANANA";
auto itr = fruitHash(fruit);
if (fruitHash.end() == itr)
{
std::cout << fruit << " not found in hash" << std::endl;
exit(1);
}
}
The FruitHash.cpp file:
#include "TupleFruits.hpp"
map_t InitializeFruitHash()
{
static map_t m;
data dBANANA = {0, 0, 6, false};
data dGRAPEFRUIT = {1, 1, 6, false};
data dSTRAWBERRY = {2, 2, 6, false};
m[BANANA] = dBANANA;
m[GRAPEFRUIT] = dGRAPEFRUIT;
m[STRAWBERRY] = dSTRAWBERRY;
return m;
}
The include file "HashData.hpp
#ifndef HASH_DATA_HPP
#define HASH_DATA_HPP
#include <string>
#include <unordered_map>
#include <cstring>
#include <iostream>
#include <tuple>
#include <boost/functional/hash.hpp>
typedef std::string fruit_key_t;
namespace HASH_TUPLES
{
struct key_hash : public std::unary_function<fruit_key_t, std::size_t>
{
std::size_t operator()(const fruit_key_t& k) const
{
std::hash<std::string> hash_fn;
return hash_fn(k);
}
};
struct key_equal : public std::binary_function<fruit_key_t, fruit_key_t, bool>
{
bool operator()(const fruit_key_t& v0, const fruit_key_t& v1) const
{
return (v0 == v1);
}
};
struct data
{
int row;
int column;
int precision;
bool isRipe;
inline bool operator ==(data d)
{
if (d.row == row && d.column == column)
return true;
else
return false;
}
friend std::ostream& operator << (std::ostream& os, const data& rhs) //Overloaded operator for '<<'
{ //for struct output
os << rhs.row << ", "
<< rhs.column;
return os;
}
};
typedef std::unordered_map<const fruit_key_t, data, key_hash, key_equal> map_t;
// ^ this is our custom hash
}
template<class T>
struct map_data_compare : public std::binary_function<typename T::value_type,
typename T::mapped_type,
bool>
{
public:
bool operator() (typename T::value_type &pair,
typename T::mapped_type i) const
{
return pair.second == i;
}
};
#endif
The include file "TupleFruits.hpp"
#ifndef TUPLESFRUITS_HPP
#define TUPLESFRUITS_HPP
#include <boost/interprocess/containers/string.hpp>
#include "HashData.hpp"
using namespace HASH_TUPLES;
map_t InitializeFruitHash();
static std::string BANANA = "banana";
static std::string GRAPEFRUIT = "grapefruit";
static std::string STRAWBERRY = "strawberry";
#endif
I figured it out. Somehow -s (strip all symbols from binary) snuck in my Makefile

Resources