Error in constexpr' expansion of 'shader.base::BasicStringPiece<char>::remove_prefix(6)' - c++11

constexpr base::StringPiece StripLambda(base::StringPiece shader) {
// Must contain at least "[]() {}".
DCHECK_EQ(shader.substr(0, 6), "[]() {");
DCHECK_EQ(shader.back(), '}');
shader.remove_prefix(6);
shader.remove_suffix(1);
return shader;
}
chromium94/src/components/viz/service/display/shader.cc:29:23: in 'constexpr' expansion of 'shader.base::BasicStringPiece::remove_prefix(6)'
chromium94/src/base/check_op.h:157:41: error: 'constexpr logging::CheckOpResult logging::CheckLEImpl(T, U, const char*) [with T = unsigned int; U = unsigned int; typename std::enable_if<(std::is_fundamental<_Tp>::value && std::is_fundamental::value), int>::type = 0]' called in a constant expression
chromium94/src/base/check_op.h:184:38: error: call to non-'constexpr' function 'logging::CheckOpResult::CheckOpResult(const char*, char*, char*)'
chromium94/src/base/check_op.h:157:41: error: 'constexpr logging::CheckOpResult logging::CheckLEImpl(T, U, const char*) [with T = unsigned int; U = unsigned int; typename std::enable_if<(std::is_fundamental<_Tp>::value && std::is_fundamental::value), int>::type = 0]' called in a constant expression
Can anyone help with work around solution?
Can anyone help with work around solution?

Related

Arrays declared globally gives out of scope error in functions

I have these arrays declared globally
char str1[] = "6541254939322816220209974565477289648317";
char str2[] = "3142522751761601737419090933147067701840";
int str1_size = strlen(str1);
int str2_size = strlen(str2);
int lcs[str1_size][str2_size];
int arrows[str1_size][str2_size];
But when I access this inside a function, it gives
lcs was not declared in this scope
also
array bound is not an integer constant before ‘]’ token
int lcs[str1_size][str2_size];
What am I doing wrong here ?
First of all
int a[dim];
where dim isn't a compile-time know constant (constexpr by example) isn't C++ standard; maybe it's possible with some extension with some compiler but isn't C++ standard.
So, in your case
int lcs[str1_size][str2_size];
int arrows[str1_size][str2_size];
where str1_size and str2_size are normal (not constexpr, not const initialized with literals) variables, is an error.
If you could redefine str1_size and str2_size as follows
constexpt int str1_size = strlen(str1);
constexpr int str2_size = strlen(str2);
the lcs and arrows definition could works.
Unfortunately (1) str1 and str2 aren't constexpr and (2) std::strlen() isn't constexpr.
But if declare str1/str2 constexpr and write a constexpr alternative to std::strlen...
#include <iostream>
constexpr std::size_t lenStr (char const * str)
{
std::size_t ret{};
while ( *(str++) )
++ret;
return ret;
}
constexpr char str1[] = "6541254939322816220209974565477289648317";
constexpr char str2[] = "3142522751761601737419090933147067701840";
constexpr auto str1_size = lenStr(str1);
constexpr auto str2_size = lenStr(str2);
int lcs[str1_size][str2_size];
int arrows[str1_size][str2_size];
int main ()
{
}
Unfortunately the preceding code require C++14 (in C++11 it's impossible write so complex constexpr functions).
In C++11 you should write lenStr() in a recursive way
constexpr std::size_t lenStr (char const * str, std::size_t ret = 0u)
{ return *str ? lenStr(++str, ++ret) : ret; }

Why isn't the iterator not identifying the datatype of the multi-set in the called function?

I'm trying to find an optimized way to this problem-
https://www.codechef.com/KJCS2019/problems/TSHACK
int findanswer(int k,const multiset<pair<int,int> >&a2)
{
int freq=0;
while(k>0)
{
multiset<pair<int,int> >::iterator itt=a2.end();
k-=itt.second;
a2.erase(itt);
++freq;
}
return freq;
}
int main()
{
cin>>n>>q;
map<int,int>a;
multiset<pair<int,int> > a2;
for(int i=0;i<n;i++)
{
int b;
cin>>b;
++a[b];
}
for(auto it:a)
a2.insert(make_pair(it.second,it.first));
while(q--)
{
int k;
cin>>k;
int ans=findanswer(k,a2);
cout<<ans<<endl;
}
}
the error message :
prog.cpp: In function 'int findanswer(int, const std::multiset<std::pair<int, int> >&)':
prog.cpp:18:16: error: 'std::multiset<std::pair<int, int> >::iterator {aka struct std::_Rb_tree_const_iterator<std::pair<int, int> >}' has no member named 'second'
k-=itt.second;
^
prog.cpp:19:21: error: passing 'const std::multiset<std::pair<int, int> >' as 'this' argument discards qualifiers [-fpermissive]
a2.erase(itt);
^
As I said earlier, I want to know the reason why the iterator in the called function not identifying the datatype of the multiset.
First problem:
You want to access the data which the iterator is pointing to and not the iterator itself. So you simply have to replace:
k-=itt->second;
Next problem:
int findanswer(int k,const multiset<pair<int,int> >&a2)
This defines your ref a2 as const. As const you can't modify your multiset. So you have to remove const here.

std::get on std::tuple results in variadic args reducing to nothing with incomplete type error

The following code produces a huge list of compiler errors:
/// Uses template recursion to bind all args
template<std::size_t N, typename... Args> class Binder
{
public:
Binder(Sqlite3StatementBase &s, std::tuple<Args...> &tup)
{
Binder<N - 1, Args...> b(s, tup);
s.bind(N + 1, std::get<N, Args...>(tup)); // Line 182
}
};
/// Specialization of Binder to end recursion at 0
template<typename... Args> class Binder<0, Args...>
{
public:
Binder(Sqlite3StatementBase &s, std::tuple<Args...> &tup)
{
s.bind(1, std::get<0, Args...>(tup));
}
};
The first batch of errors consists of:
In file included from /usr/include/c++/6/bits/unique_ptr.h:37:0,
from /usr/include/c++/6/condition_variable:43,
from /home/tony/htpc/Dev/logi/src/db/logi-db.h:22,
from /home/tony/htpc/Dev/logi/src/db/logi-sqlite.h:26,
from /home/tony/htpc/Dev/logi/src/db/logi-sqlite.cpp:24:
/usr/include/c++/6/tuple: In instantiation of ‘class std::tuple_element<1ul, std::tuple<unsigned int> >’:
/usr/include/c++/6/tuple:1228:12: recursively required from ‘class std::tuple_element<2ul, std::tuple<unsigned int, unsigned int> >’
/usr/include/c++/6/tuple:1228:12: required from ‘class std::tuple_element<3ul, std::tuple<unsigned int, unsigned int, unsigned int> >’
/usr/include/c++/6/utility:106:69: required by substitution of ‘template<long unsigned int __i, class _Tp> using __tuple_element_t = typename std::tuple_element::type [with long unsigned int __i = 3ul; _Tp = std::tuple<unsigned int, unsigned int, unsigned int>]’
/usr/include/c++/6/tuple:1270:5: required by substitution of ‘template<long unsigned int __i, class ... _Elements> constexpr std::__tuple_element_t<__i, std::tuple<_Elements ...> >&& std::get(std::tuple<_Elements ...>&&) [with long unsigned int __i = 3ul; _Elements = {unsigned int, unsigned int, unsigned int}]’
/home/tony/htpc/Dev/logi/src/db/logi-sqlite.h:182:47: required from ‘logi::Sqlite3Database::Binder<N, Args>::Binder(logi::Sqlite3Database::Sqlite3StatementBase&, std::tuple<_Elements ...>&) [with long unsigned int N = 3ul; Args = {unsigned int, unsigned int, unsigned int}]’
/home/tony/htpc/Dev/logi/src/db/logi-sqlite.h:238:71: required from ‘void logi::Sqlite3Database::Sqlite3Statement<Args>::prepare_row(logi::Sqlite3Database::Sqlite3Statement<Args>::Tup&) [with Args = {unsigned int, unsigned int, unsigned int}; logi::Sqlite3Database::Sqlite3Statement<Args>::Tup = std::tuple<unsigned int, unsigned int, unsigned int>]’
/home/tony/htpc/Dev/logi/src/db/logi-sqlite.h:231:28: required from ‘void logi::Sqlite3Database::Sqlite3Statement<Args>::execute(typename logi::Sqlite3Database::Sqlite3Statement<Args>::Parent::ArgsVector&) [with Args = {unsigned int, unsigned int, unsigned int}; typename logi::Sqlite3Database::Sqlite3Statement<Args>::Parent::ArgsVector = std::vector<std::tuple<unsigned int, unsigned int, unsigned int>, std::allocator<std::tuple<unsigned int, unsigned int, unsigned int> > >]’
/home/tony/htpc/Dev/logi/src/db/logi-sqlite.cpp:354:1: required from here
/usr/include/c++/6/tuple:1228:12: error: invalid use of incomplete type ‘class std::tuple_element<0ul, std::tuple<> >’
So it seems as if the std::get call is causing other parts of the system libraries (not directly related to std::get or std::tuple) to recursively reduce Args... to nothing, and this is happening independently of my recursion of the numerical template parameter N. Is there something I can fix in my code without fundamentally changing my approach, or is it just not possible to use std::get in a context where the tuple's template parameters are variadic?
Given an implementation of the C++14 make_index_sequence function (and its associated types), you can do what you want easily enough:
template<size_t ...indices, typename ...Args>
void binder_helper(Sqlite3StatementBase &s, std::integer_sequence<size_t, indices...>, std::tuple<Args...> &tup)
{
auto dump = {(s.bind(indices, std::get<indices>(tup)), 0)...};
}
template<typename ...Args>
void binder(Sqlite3StatementBase &s, std::tuple<Args...> &tup)
{
binder_helper(s, std::make_index_sequence<sizeof...(Args)>(), tup);
}
With C++17's fold expressions, the oddball stuff in binder_helper can be reduced to something much more reasonable:
template<size_t ...indices, typename ...Args>
void binder_helper(Sqlite3StatementBase &s, std::integer_sequence<size_t, indices...>, std::tuple<Args...> &tup)
{
(s.bind(indices, std::get<indices>(tup)), ...);
}
Note that in the latter case, the standard does not guarantee thatt the calls to s.bind will be executed in order. In the former case, because the expressions are wrapped in a braced-init-list, you're guaranteed in-order evaluation.

unordered_map with IP address as a key

I am trying to build a list of linked list in my application. The list will contains unique IP addresses,and for each IP addres I have a list of applications. I am trying to build it using unordered_map to take Boost::boost::asio::ip::address as a the key and std::list as value:
#include <boost/unordered/unordered_map.hpp>
#include <iostream>
#include <list>
using namespace boost::asio::ip;
using namespace std;
typedef int ApplicationID;
typedef address IPAddress;
typedef list <ApplicationID> APP_LIST;
typedef boost::unordered::unordered_map <IPAddress, APP_LIST> USER_MAP;
USER_MAP user_map;
Later I try to get the list associated with an IP address as following:
APP_LIST *list = &user_map[ip];
But I get errors in the compilation, so could you please specify what's the problem?
Is it possible to use Boost:IPaddress as a key function?
Another question is it possible to use char[some_size] as a key value?
Error Output:
In file included from /boost/functional/hash/hash.hpp:535:0,
from /boost/functional/hash.hpp:6,
from /boost/unordered/unordered_map.hpp:21,
from ipc_module.cpp:18:
/boost/functional/hash/extensions.hpp: In member function ‘std::size_t boost::hash<T>::operator()(const T&) const [with T = boost::asio::ip::address, std::size_t = long unsigned int]’:
/boost/unordered/detail/unique.hpp:331:55: instantiated from ‘boost::unordered::detail::table_impl<Types>::value_type& boost::unordered::detail::table_impl<Types>::operator[](const key_type&) [with Types = boost::unordered::detail::map<std::allocator<std::pair<const boost::asio::ip::address, std::list<int> > >, boost::asio::ip::address, std::list<int>, boost::hash<boost::asio::ip::address>, std::equal_to<boost::asio::ip::address> >, boost::unordered::detail::table_impl<Types>::value_type = std::pair<const boost::asio::ip::address, std::list<int> >, boost::unordered::detail::table_impl<Types>::key_type = boost::asio::ip::address]’
/boost/unordered/unordered_map.hpp:1192:26: instantiated from ‘boost::unordered::unordered_map<K, T, H, P, A>::mapped_type& boost::unordered::unordered_map<K, T, H, P, A>::operator[](const key_type&) [with K = boost::asio::ip::address, T = std::list<int>, H = boost::hash<boost::asio::ip::address>, P = std::equal_to<boost::asio::ip::address>, A = std::allocator<std::pair<const boost::asio::ip::address, std::list<int> > >, boost::unordered::unordered_map<K, T, H, P, A>::mapped_type = std::list<int>, boost::unordered::unordered_map<K, T, H, P, A>::key_type = boost::asio::ip::address]’
ipc_module.cpp:175:40: instantiated from here
/boost/functional/hash/extensions.hpp:176:34: error: no matching function for call to ‘hash_value(const boost::asio::ip::address&)’
/boost/functional/hash/extensions.hpp:176:34: note: candidates are:
/boost/smart_ptr/shared_ptr.hpp:708:33: note: template<class T> std::size_t boost::hash_value(const boost::shared_ptr<T>&)
/boost/functional/hash/hash.hpp:144:24: note: std::size_t boost::hash_value(bool)
/boost/functional/hash/hash.hpp:144:24: note: no known conversion for argument 1 from ‘const boost::asio::ip::address’ to ‘bool’
/boost/functional/hash/hash.hpp:149:24: note: std::size_t boost::hash_value(char)
/boost/functional/hash/hash.hpp:149:24: note: no known conversion for argument 1 from ‘const boost::asio::ip::address’ to ‘char’
/boost/functional/hash/hash.hpp:154:24: note: std::size_t boost::hash_value(unsigned char)
/boost/functional/hash/hash.hpp:154:24: note: no known conversion for argument 1 from ‘const boost::asio::ip::address’ to ‘unsigned char’
/boost/functional/hash/hash.hpp:159:24: note: std::size_t boost::hash_value(signed char)
/boost/functional/hash/hash.hpp:159:24: note: no known conversion for argument 1 from ‘const boost::asio::ip::address’ to ‘signed char’
/boost/functional/hash/hash.hpp:164:24: note: std::size_t boost::hash_value(short int)
/boost/functional/hash/hash.hpp:164:24: note: no known conversion for argument 1 from ‘const boost::asio::ip::address’ to ‘short int’
/boost/functional/hash/hash.hpp:169:24: note: std::size_t boost::hash_value(short unsigned int)
/boost/functional/hash/hash.hpp:169:24: note: no known conversion for argument 1 from ‘const boost::asio::ip::address’ to ‘short unsigned int’
/boost/functional/hash/hash.hpp:174:24: note: std::size_t boost::hash_value(int)
/boost/functional/hash/hash.hpp:174:24: note: no known conversion for argument 1 from ‘const boost::asio::ip::address’ to ‘int’
/boost/functional/hash/hash.hpp:179:24: note: std::size_t boost::hash_value(unsigned int)
/boost/functional/hash/hash.hpp:179:24: note: no known conversion for argument 1 from ‘const boost::asio::ip::address’ to ‘unsigned int’
/boost/functional/hash/hash.hpp:184:24: note: std::size_t boost::hash_value(long int)
/boost/functional/hash/hash.hpp:184:24: note: no known conversion for argument 1 from ‘const boost::asio::ip::address’ to ‘long int’
/boost/functional/hash/hash.hpp:189:24: note: std::size_t boost::hash_value(long unsigned int)
/boost/functional/hash/hash.hpp:189:24: note: no known conversion for argument 1 from ‘const boost::asio::ip::address’ to ‘long unsigned int’
/boost/functional/hash/hash.hpp:195:24: note: std::size_t boost::hash_value(wchar_t)
/boost/functional/hash/hash.hpp:195:24: note: no known conversion for argument 1 from ‘const boost::asio::ip::address’ to ‘wchar_t’
/boost/functional/hash/hash.hpp:202:24: note: std::size_t boost::hash_value(boost::long_long_type)
/boost/functional/hash/hash.hpp:202:24: note: no known conversion for argument 1 from ‘const boost::asio::ip::address’ to ‘boost::long_long_type {aka long long int}’
/boost/functional/hash/hash.hpp:207:24: note: std::size_t boost::hash_value(boost::ulong_long_type)
/boost/functional/hash/hash.hpp:207:24: note: no known conversion for argument 1 from ‘const boost::asio::ip::address’ to ‘boost::ulong_long_type {aka long long unsigned int}’
/boost/functional/hash/hash.hpp:215:36: note: template<class T> std::size_t boost::hash_value(T* const&)
/boost/functional/hash/hash.hpp:308:24: note: template<class T, unsigned int N> std::size_t boost::hash_value(const T (&)[N])
/boost/functional/hash/hash.hpp:314:24: note: template<class T, unsigned int N> std::size_t boost::hash_value(T (&)[N])
/boost/functional/hash/hash.hpp:327:24: note: std::size_t boost::hash_value(float)
/boost/functional/hash/hash.hpp:327:24: note: no known conversion for argument 1 from ‘const boost::asio::ip::address’ to ‘float’
/boost/functional/hash/hash.hpp:332:24: note: std::size_t boost::hash_value(double)
/boost/functional/hash/hash.hpp:332:24: note: no known conversion for argument 1 from ‘const boost::asio::ip::address’ to ‘double’
/boost/functional/hash/hash.hpp:337:24: note: std::size_t boost::hash_value(long double)
/boost/functional/hash/hash.hpp:337:24: note: no known conversion for argument 1 from ‘const boost::asio::ip::address’ to ‘long double’
/boost/functional/hash/hash.hpp:321:24: note: template<class Ch, class A> std::size_t boost::hash_value(const std::basic_string<Ch, std::char_traits<_CharT>, A>&)
/boost/functional/hash/hash.hpp:343:24: note: std::size_t boost::hash_value(std::type_index)
/boost/functional/hash/hash.hpp:343:24: note: no known conversion for argument 1 from ‘const boost::asio::ip::address’ to ‘std::type_index’
/boost/functional/hash/extensions.hpp:54:17: note: template<class A, class B> std::size_t boost::hash_value(const std::pair<_T1, _T2>&)
/boost/functional/hash/extensions.hpp:63:17: note: template<class T, class A> std::size_t boost::hash_value(const std::vector<_Tp, _Alloc>&)
/boost/functional/hash/extensions.hpp:69:17: note: template<class T, class A> std::size_t boost::hash_value(const std::list<_Tp, _Alloc>&)
/boost/functional/hash/extensions.hpp:75:17: note: template<class T, class A> std::size_t boost::hash_value(const std::deque<_Tp, _Alloc>&)
/boost/functional/hash/extensions.hpp:81:17: note: template<class K, class C, class A> std::size_t boost::hash_value(const std::set<_Key, _Compare, _Alloc>&)
/boost/functional/hash/extensions.hpp:87:17: note: template<class K, class C, class A> std::size_t boost::hash_value(const std::multiset<_Key, _Compare, _Alloc>&)
/boost/functional/hash/extensions.hpp:93:17: note: template<class K, class T, class C, class A> std::size_t boost::hash_value(const std::map<_Key, _Tp, _Compare, _Alloc>&)
/boost/functional/hash/extensions.hpp:99:17: note: template<class K, class T, class C, class A> std::size_t boost::hash_value(const std::multimap<_Key, _Tp, _Compare, _Alloc>&)
/boost/functional/hash/extensions.hpp:105:17: note: template<class T> std::size_t boost::hash_value(const std::complex<_Tp>&)
/boost/functional/hash/extensions.hpp:177:9: warning: control reaches end of non-void function [-Wreturn-type]
/boost/asio/error.hpp: At global scope:
/boost/asio/error.hpp:244:45: warning: ‘boost::asio::error::system_category’ defined but not used [-Wunused-variable]
/boost/asio/error.hpp:246:45: warning: ‘boost::asio::error::netdb_category’ defined but not used [-Wunused-variable]
/boost/asio/error.hpp:248:45: warning: ‘boost::asio::error::addrinfo_category’ defined but not used [-Wunused-variable]
/boost/asio/error.hpp:250:45: warning: ‘boost::asio::error::misc_category’ defined but not used [-Wunused-variable]
Here's what I came up with after a brief inspection of the class interface for ip::address.
I would like to note that it's pretty wasteful to use. Especially if you happen to know that all addresses are ipv4, e.g. I'd prefer to key by ulong then.
namespace boost
{
template <>
struct hash<IPAddress>
{
size_t operator()(IPAddress const& v) const {
if (v.is_v4())
return v.to_v4().to_ulong();
if (v.is_v6())
{
auto const& range = v.to_v6().to_bytes();
return hash_range(range.begin(), range.end());
}
if (v.is_unspecified())
{
// guaranteed to be random: chosen by fair dice roll
return static_cast<size_t>(0x4751301174351161ul);
}
return hash_value(v.to_string());
}
};
}
See it Live on Coliru:
#include <boost/asio.hpp>
#include <boost/unordered/unordered_map.hpp>
#include <iostream>
#include <list>
typedef int ApplicationID;
typedef boost::asio::ip::address IPAddress;
typedef std::list<ApplicationID> APP_LIST;
typedef boost::unordered::unordered_map<IPAddress, APP_LIST> USER_MAP;
namespace boost
{
template <>
struct hash<IPAddress>
{
size_t operator()(IPAddress const& v) const {
if (v.is_v4())
return v.to_v4().to_ulong();
if (v.is_v6())
{
auto const& range = v.to_v6().to_bytes();
return hash_range(range.begin(), range.end());
}
if (v.is_unspecified())
return 0x4751301174351161ul;
return hash_value(v.to_string());
}
};
}
int main()
{
USER_MAP map;
map.insert({ {}, {} });
}
This is an old question, but I recently came across the same issue while writing a game in c++17.
This is how I solved it:
struct endpoint_hash
{
std::size_t operator()(boost::asio::ip::udp::endpoint const& ep) const noexcept
{
auto accum = std::size_t(0);
auto combine = [&accum](auto&& arg) {
boost::hash_combine(accum, arg);
};
combine(ep.port());
if (auto&& addr = ep.address(); addr.is_v4())
{
combine(addr.to_v4().to_ulong());
}
else
{
combine(addr.to_v6().to_bytes());
}
combine(ep.port());
return accum;
}
};
std::unordered_map<boost::asio::ip::udp::endpoint, std::shared_ptr<game_client>, endpoint_hash> cache_;
It will work if you write a hash function for IPaddress.

string literal parameter not accepted to a constexpr function

Call to the extract function below does not work for me on g++ 4.9.0 (20130421). The error I get is that s1 is not a constant expression. If i can be initialized as constexpr then j and k should too. Is that wrong?
#include <tuple>
template <unsigned N1, unsigned N2>
constexpr bool strmatch(const char (&s1)[N1], const char (&s2)[N2], unsigned i = 0)
{
return (s1[i]==s2[i]) ?
(s1[i]=='\0') ?
true
: strmatch(s1, s2, i+1)
: false;
}
template<unsigned N>
constexpr int extract(const std::tuple<int, int> & t1, const char (&array)[N]) {
return std::get<strmatch(array, "m0")>(t1);
}
int main(void)
{
constexpr int i = strmatch("m0", "m0"); // OK
constexpr int j = extract(std::make_tuple(10, 20), "m0");
constexpr int k = extract(std::make_tuple(10, 20), "m1");
return 0;
}
Your code is ill-formed. The problem is that array is not a core constant expression, so can't be used in the template argument in the call to std::get:
template<unsigned N>
constexpr int extract(const std::tuple<int, int> & t1, const char (&array)[N]) {
return std::get<strmatch(array, "m0")>(t1);
}
Remember that constexpr functions can be called at runtime: this code would use the value of a runtime parameter to this function (array) during translation (in the evaluation of the call to strmatch).

Resources