CTYPE issue during building dame - c++

I am trying to build DAME. Environment details:
GCC - gcc version 4.4.2 (GCC)
ANTLR - ANTLR Parser Generator Version 2.7.2
when i am trying to compile the following code snippet:
#include <functional>
#include <iomanip>
struct compare_without_case_char : public std::binary_function<char, char, bool>
{
const std::ctype<char>& ct ;
compare_without_case_char (const std::ctype<char>& c = std::use_facet<std::ctype<char> > (std::locale::classic()))
: ct(c) {}
bool operator() (char x, char y) const
{
return (ct.toupper(x) == ct.toupper(y)) ;
}
} ;
Following error is coming:
test_ctype.cpp: In member function 'bool compare_without_case_char::operator()(char, char) const':
test_ctype.cpp:16: error: invalid use of incomplete type 'const struct std::ctype<char>'
/storage11/oracle/ANAND_BM/gcc-4.4.2/gcc_4_4_2_release/gcc-4.4.2_build/lib/gcc/sparc-sun-solaris2.10/4.4.2/../../../../include/c++/4.4.2/bits/localefwd.h:115: error: declaration of 'const struct std::ctype<char>'
test_ctype.cpp:16: error: invalid use of incomplete type 'const struct std::ctype<char>'
/storage11/oracle/ANAND_BM/gcc-4.4.2/gcc_4_4_2_release/gcc-4.4.2_build/lib/gcc/sparc-sun-solaris2.10/4.4.2/../../../../include/c++/4.4.2/bits/localefwd.h:115: error: declaration of 'const struct std::ctype<char>'
In file included from /storage11/oracle/ANAND_BM/gcc-4.4.2/gcc_4_4_2_release/gcc-4.4.2_build/lib/gcc/sparc-sun-solaris2.10/4.4.2/../../../../include/c++/4.4.2/bits/locale_classes.h:809,
from /storage11/oracle/ANAND_BM/gcc-4.4.2/gcc_4_4_2_release/gcc-4.4.2_build/lib/gcc/sparc-sun-solaris2.10/4.4.2/../../../../include/c++/4.4.2/bits/ios_base.h:43,
from /storage11/oracle/ANAND_BM/gcc-4.4.2/gcc_4_4_2_release/gcc-4.4.2_build/lib/gcc/sparc-sun-solaris2.10/4.4.2/../../../../include/c++/4.4.2/iomanip:42,
from test_ctype.cpp:2:
/storage11/oracle/ANAND_BM/gcc-4.4.2/gcc_4_4_2_release/gcc-4.4.2_build/lib/gcc/sparc-sun-solaris2.10/4.4.2/../../../../include/c++/4.4.2/bits/locale_classes.tcc: In function 'const _Facet& std::use_facet(const std::locale&) [with _Facet = std::ctype<char>]':
test_ctype.cpp:11: instantiated from here
/storage11/oracle/ANAND_BM/gcc-4.4.2/gcc_4_4_2_release/gcc-4.4.2_build/lib/gcc/sparc-sun-solaris2.10/4.4.2/../../../../include/c++/4.4.2/bits/locale_classes.tcc:107: error: incomplete type 'std::ctype<char>' used in nested name specifier
/storage11/oracle/ANAND_BM/gcc-4.4.2/gcc_4_4_2_release/gcc-4.4.2_build/lib/gcc/sparc-sun-solaris2.10/4.4.2/../../../../include/c++/4.4.2/bits/locale_classes.tcc:112: error: cannot dynamic_cast '* *(__facets + ((unsigned int)(((unsigned int)__i) * 4u)))' (of type 'const class std::locale::facet') to type 'const struct std::ctype<char>&' (target is not pointer or reference to complete type)
What is the real issue and how I can resolve it.
Regards,
Anand Kumar Keshri

The only problem here is that your compiler cannot find std::ctype. You have to #include the file containing it :
#include <locale>

Related

conversion from '_Complex float' to 'int' is not allowed in a converted constant expression

I am using protobuf to exchange some messages but when i try to compile the code that uses the messages i am having this conversion error in the repeated field.h file, specifically in the code below.
Is it a version problem ?
.proto file
message mymessage {
repeated double message = 20;
}
protobuf repeated field.h
template <int I>
class FastAdderImpl<I, false> {
public:
explicit FastAdderImpl(RepeatedField* rf) : repeated_field_(rf) {}
void Add(const Element& val) { repeated_field_->Add(val); }
private:
RepeatedField* repeated_field_;
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FastAdderImpl);
};
Error:
/usr/local/include/google/protobuf/repeated_field.h:473:17: error: expected ')'
template <int I>
^
/usr/include/complex.h:53:11: note: expanded from macro 'I'
#define I _Complex_I
^
/usr/include/complex.h:48:21: note: expanded from macro '_Complex_I'
#define _Complex_I (__extension__ 1.0iF)
^
/usr/local/include/google/protobuf/repeated_field.h:473:17: note: to match this '('
/usr/include/complex.h:53:11: note: expanded from macro 'I'
#define I _Complex_I
^
/usr/include/complex.h:48:20: note: expanded from macro '_Complex_I'
#define _Complex_I (__extension__ 1.0iF)
^
error: conversion from '_Complex float' to 'int' is not allowed in a converted constant expression
class FastAdderImpl<I, false> {
^
/usr/local/include/google/protobuf/repeated_field.h:474:23: error: conversion from '_Complex float' to 'int' is not allowed in a converted constant expression
class FastAdderImpl<I, false> {
^
/usr/include/complex.h:53:11: note: expanded from macro 'I'
#define I _Complex_I
^~~~~~~~~~
/usr/include/complex.h:48:20: note: expanded from macro '_Complex_I'
#define _Complex_I (__extension__ 1.0iF)
^~~~~~~~~~~~~~~~~~~~~
2 errors generated.
The problem is that a non-type template argument must be a compile-time constant but the type of the non-type template parameter(int) in your example is not the same as the type of the passed argument((__extension__ 1.0iF)) and so requires a conversion.
Perhaps a contrived example might clear this up further:
constexpr float k = 4.4f;
template<int I>
void f()
{
}
int main()
{
//----v----->not valid and will produce a similar error:conversion from 'float' to 'int' in a converted constant expression
f<k>();
}
The above will produce a similar error saying:
conversion from 'float' to 'int' in a converted constant expression
could not convert 'k' from 'const float' to 'int'
Demo

c++, what happens when an lvalue is passed to T&&?

#include <bits/stdc++.h>
#include <vector>
template <typename T>
void g(T&& val)
{
std::vector<T> v;
}
int main()
{
// g(2);
int i;
g(i);
}
When g(2) is called, it complies, but when g(i) is called, the complier has a lot of errors.
Some of the errors are pasted as follows:
forming pointer to reference type ‘int&’
no members matching ‘std::vector<int&, std::allocator<int&> >::_Base {aka std::_Vector_base<int&, std::allocator<int&> >}::_M_allocate’ in ‘std::vector<int&, std::allocator<int&> >::_Base’ {aka ‘struct std::_Vector_base<int&, std::allocator<int&> >’}
no members matching ‘__gnu_cxx::__alloc_traits<std::allocator<int&>, int&>::_Base_type {aka std::allocator_traits<std::allocator<int&> >}::allocate’ in ‘__gnu_cxx::__alloc_traits<std::allocator<int&>, int&>::_Base_type’ {aka ‘struct std::allocator_traits<std::allocator<int&> >’}
forming pointer to reference type ‘int&’
my question:
1. In this case, when the literal value 2 is called, what type is derived for T ?
2. Why doesn't it complie when on i ? How to understand these error messages ?
1: A forwarding reference, and
g(2) makes T an int
g(i) makes T an int&
2: You can't have arrays of references (new T&[x]). You could use std::remove_cvref_t to get the type int out of T:
#include <type_traits>
template <typename T>
void g(T&& val) {
using type = std::remove_cvref_t<T>; // type is int
std::vector<type> v;
}
Your universal reference resolves to int for g(2) and int& for g(i)
You can't have a std::vector of a reference type (see this explanation).

Cannot compile variant visitor access on MSVC 19.28

I try to compile a personal project on Visual Studio 2019 (using MSVC 19.28 compiler) and I came accross a compilation error in the std::visit which I don't understand:
<source>(131): error C2653: '`global namespace'': is not a class or namespace name
C:/data/msvc/14.28.29914/include\type_traits(1493): note: see reference to function template instantiation 'auto CommandLineOptionsParser<CmdLineOpts>::register_callback::<lambda_1>::()::<lambda_1>::operator ()<const _First&>(_T1) const' being compiled
with
[
_First=bool CmdLineOpts::* ,
_T1=bool CmdLineOpts::* const &
]
C:/data/msvc/14.28.29914/include\variant(1654): note: see reference to alias template instantiation 'std::_Variant_visit_result_t<CommandLineOptionsParser<CmdLineOpts>::register_callback::<lambda_1>::()::<lambda_1>,const std::variant<bool CmdLineOpts::* >&>' being compiled
<source>(120): note: while compiling class template member function 'void CommandLineOptionsParser<CmdLineOpts>::register_callback(const CommandLineOption &,std::variant<bool CmdLineOpts::* >)'
<source>(83): note: see reference to function template instantiation 'void CommandLineOptionsParser<CmdLineOpts>::register_callback(const CommandLineOption &,std::variant<bool CmdLineOpts::* >)' being compiled
<source>(142): note: see reference to class template instantiation 'CommandLineOptionsParser<CmdLineOpts>' being compiled
<source>(123): error C2672: 'visit': no matching overloaded function found
<source>(131): error C2893: Failed to specialize function template 'unknown-type std::visit(_Callable &&,_Variants &&...)'
C:/data/msvc/14.28.29914/include\variant(1654): note: see declaration of 'std::visit'
<source>(131): note: With the following template arguments:
<source>(131): note: '_Callable=CommandLineOptionsParser<CmdLineOpts>::register_callback::<lambda_1>::()::<lambda_1>'
<source>(131): note: '_Variants={const std::variant<bool CmdLineOpts::* > &}'
<source>(131): note: '<unnamed-symbol>=void'
Compiler returned: 2
This code compiles fine with gcc.
I tested the code snippet from cppreference on std::visit and it compiles with MSVC, so I am not so sure what the issue here.
I simplified the code and reproduced the issue on godbolt
Here's the code
#include <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <sstream>
#include <string>
#include <set>
#include <string_view>
#include <variant>
#include <type_traits>
using InvalidArgumentException = std::invalid_argument;
using CommandLineOption = std::string;
template <class Opts>
class CommandLineOptionsParser : Opts {
public:
using OptionType = std::variant<bool Opts::*>;
using CommandLineOptionWithValue = std::pair<CommandLineOption, OptionType>;
Opts parse(const char* argStr) {
// First register the callbacks
bool Opts::* pBool = &Opts::help;
register_callback("help", pBool);
for (auto& cbk : _callbacks) {
cbk.second(0, argStr);
}
return static_cast<Opts>(*this);
}
private:
using callback_t = std::function<void(int, const char *)>;
std::map<CommandLineOption, callback_t> _callbacks;
void register_callback(const CommandLineOption& commandLineOption, OptionType prop) {
_callbacks[commandLineOption] = [this, &commandLineOption, prop](int idx, const char * argv) {
if (std::string(argv) == commandLineOption) {
std::visit([](auto&& a) {
using T = std::decay_t<decltype(a)>;
if constexpr (std::is_same_v<T, bool Opts::*>) {
std::cout << "bool" << std::endl;
}
},
prop);
}
};
};
};
struct CmdLineOpts {
bool help{};
};
int main(int argc, const char* argv[])
{
CommandLineOptionsParser<CmdLineOpts> p;
CmdLineOpts cmdLineOptions = p.parse("opt1");
}
It seems MSVC is having difficulty synthesizing a lambda with a pointer-to-member argument in a template context.
I tried to simplify it to a MCVE, hopefully it captures the essence of the issue:
template<class T>
bool test(int T::* t) {
return [](int T::* x) {
return true;
}(t);
}
struct A {
int a;
};
int main() {
return test<A>(&A::a);
}
It fails to compile in MSVC C++20 mode (but not C++17) with a similar nonsensical error (link):
<source>(5): error C2653: '`global namespace'': is not a class or namespace name
<source>(13): note: see reference to function template instantiation 'bool test<A>(int A::* )' being compiled
<source>(5): error C2664: 'bool test::<lambda_1>::operator ()(A *) const': cannot convert argument 1 from 'int A::* ' to 'A *'
<source>(5): note: There is no context in which this conversion is possible
<source>(5): note: see declaration of 'test::<lambda_1>::operator ()'
I would suggest to report this to the vendor.
As a potential workaround can try extracting the lambda into a functor class with a templated operator(), it seems to compile (example).

C++11 decltype: How to declare the type that a pointer points to?

I have the following code:
#include <memory>
int main()
{
int* a = new int(2);
std::unique_ptr<decltype(*a)> p(a);
}
which leads to these error message:
In file included from a.cpp:1:
In file included from /usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/memory:81:
/usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/bits/unique_ptr.h:138:14: error: '__test' declared as a pointer to a reference of type 'int &'
static _Tp* __test(...);
^
/usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/bits/unique_ptr.h:146:35: note: in instantiation of member class 'std::unique_ptr<int &,
std::default_delete<int &> >::_Pointer' requested here
typedef std::tuple<typename _Pointer::type, _Dp> __tuple_type;
^
a.cpp:7:35: note: in instantiation of template class 'std::unique_ptr<int &, std::default_delete<int &> >' requested here
std::unique_ptr<decltype(*a)> p(a);
^
In file included from a.cpp:1:
In file included from /usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/memory:81:
/usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/bits/unique_ptr.h:227:33: error: 'type name' declared as a pointer to a reference of type 'int &'
is_convertible<_Up*, _Tp*>, is_same<_Dp, default_delete<_Tp>>>>
^
a.cpp:7:35: note: in instantiation of template class 'std::unique_ptr<int &, std::default_delete<int &> >' requested here
std::unique_ptr<decltype(*a)> p(a);
^
2 errors generated.
I understand the reason is that the unique_ptr template expects type int, but decltype(*a) gives int&. In the case that int is a very long and complicated type, how can I make this code work with decltype?
Use std::decay_t. This is the conversion that is applied when you pass an argument to a function by value.
You can use a typedef inside a templated class and then use template specialisation, like this
template<typename T> struct unref {
typedef T raw;
};
template<typename T> struct unref<T&> {
typedef T raw;
};
int main() {
int* a = new int(2);
std::unique_ptr<unref<decltype(*a)>::raw> p(a);
}

What is causing this template-related compile error?

When I try to compile this:
#include <map>
#include <string>
template <class T>
class ZUniquePool
{
typedef std::map< int, T* > ZObjectMap;
ZObjectMap m_objects;
public:
T * Get( int id )
{
ZObjectMap::const_iterator it = m_objects.find( id );
if( it == m_objects.end() )
{
T * p = new T;
m_objects[ id ] = p;
return p;
}
return m_objects[ id ];
}
};
int main( int argc, char * args )
{
ZUniquePool< std::string > pool;
return 0;
}
I get this:
main.cpp: In member function ‘T* ZUniquePool<T>::Get(int)’:
main.cpp:12: error: expected `;' before ‘it’
main.cpp:13: error: ‘it’ was not declared in this scope
I'm using GCC 4.2.1 on Mac OS X.
It works in VS2008.
I'm wondering whether it might be a variation of this problem:
Why doesn't this C++ template code compile?
But as my error output is only partially similar, and my code works in VS2008, I am not sure.
Can anyone shed some light on what I am doing wrong?
You need typename:
typename ZObjectMap::const_iterator it = m_objects.find( id )
Since the type of ZObjectMap is dependent on the template parameter, the compiler has no clue what ZObjectMap::const_iterator is (it could be a member variable). You need to use typename to inform the compiler to assume it will be some sort of type.
Also to mention, GCC 4.5.0 gives the following output which would help you to solve the problem:
main.cpp: In member function 'T* ZUniquePool<T>::Get(int)':
main.cpp:12:7: error: need 'typename' before 'ZUniquePool<T>::ZObjectMap:: const_iterator' because 'ZUniquePool<T>::ZObjectMap' is a dependent scope
main.cpp:12:34: error: expected ';' before 'it'
main.cpp:13:11: error: 'it' was not declared in this scope
main.cpp: At global scope:
main.cpp:23:5: warning: second argument of 'int main(int, char*)' should be 'char **'