jeudi 28 décembre 2017

Difference between const vector and const iterator

If I am passing a vector to a function as reference and I want the function should not modify the vector, what is better - using const vector<> or using vector::const_iterator?

For example, I am traversing through a vector passed from main() to foo().

void foo (const vector<int> &v1) {
vector<int>::const_iterator m;

for(m=v1.begin();m1!=v1.end();++m1)
//loop body

}

And main() is

int main() {
vector<int> v11={0,1,2,3,4};
foo(v11);
}

In this case, v1 should be const or m should be const_iterator?

C++ list is working but giving error Thread 1: EXC_BAD_ACCESS (code=1, address=0x8)

Hi Guys i am facing this list problem where i want to clone a list with random pointers. But my code is giving me this error while cloning pointers. here i am adding only the function code but i am also attaching the link for full code. the error is

Thread 1: EXC_BAD_ACCESS (code=1, address=0x8)

Node *createAnotherList(Node *head){
Node *temp = head, *start = head;
Node *firstPointer;
int count = 0;

while(temp != NULL){
    Node *newNode = new Node;
    newNode->data = temp->data;
    newNode->random = NULL;
    newNode->next = temp->next;
    temp->next = newNode;
    if(count == 0){
        firstPointer = newNode;
        count++;
    }
    temp = newNode->next;
}

//    printList(list1);

while(start != NULL){
    cout << start->data << " ";
    if(start->next)
        start->next->random = start->random->next;
    start = start->next ? start->next->next : start->next;

}

    return head;
}

i am getting error in 2nd while loop

start->next->random = start->random->next;

if someone knows what is the error please help me. and for more info please check the code from the link and error line is LINE 74

enter link description here

Check if all std::tuple elements meet a condition + design concerns

So I am trying to validate if all elements of a std::tuple meet a certain condition. My current solution uses C++17 fold epxressions:

template <typename F, typename Tuple, size_t...Is>
_CR_INLINE bool tuple_all_of(F&& fn, Tuple&& t, std::index_sequence<Is...>) {
    return (std::forward<F>(fn)(std::get<Is>(std::forward<Tuple>(t))) && ...);
}

template <typename F, typename Tuple>
_CR_INLINE bool tuple_all_of(F&& fn, Tuple&& t) {
    return tuple_all_of(std::forward<F>(fn), std::forward<Tuple>(t), 
        std::make_index_sequence<std::tuple_size_v<std::remove_reference_t<Tuple>>>());
}

It compiles in latest clang and gcc, but fails in MSVC, because fold expressions are not (yet) implemented. (http://ift.tt/2q3O3xq)

So is there a similar solution without using fold expressions?


This is how it's supposed to be used:

template<typename...Components, typename Callable>
    void FindComponents(Callable&& fn) {
        for (auto& node : m_Entities) {
            auto tp = std::make_tuple(node.GetComponent<Components>()...);
            if (!tuple_all_of([](auto& p) { return p != nullptr; }, tp))
                continue;

            apply_from_tuple(fn, tp);
        }
    }

So I am calling fn only if a node has the requested components attached (i.e. GetComponent != nullptr). Maybe there is a better solution?

Creating header file with function

I am trying to create function in separate header file but getting problem. Here is the code.

//printVec.cpp
#include<vector>
#include<iostream>
#include"printV.h"

void printVec(const std::vector<double> *v1)
{
    std::vector<double>::const_iterator ele;

    std::cout << "elements: \n";

    for (ele = v1->begin(); ele != v1->end(); ++ele)
    {
        std::cout << *ele << " ";
    }
}

//func1.cpp
#include <iostream>
#include<vector>
#include"printV.h"

int main()
{
    std::vector<double> A={1,2,3,4,5,6,7,8,9,10};
    //Displaying vector A and B
    printVec(&A);
    return 0;
}

This is header file.

//printV.h
#ifndef PRINTV_H
#define PRINTV_H
#include<vector>
void printVec(const std::vector<double> *v1);

#endif

I am compiling it using g++ -Wall -Wextra -I <path to header file> printVec.cpp func1.cpp -o prog_matmult -std=c++11, but getting error as undefined reference toprintVec(std::vector > const*)' collect2.exe: error: ld returned 1 exit status `. Can someone comment what is problematic?

SFINAE-enabled function template getting instantiated twice and each time differently

I've designed a pretty print/format library using template meta-programming that could print/format objects of any type, as long as they support certain concept(s). For that, I've few overloaded function templates (sfinae-enabled) and I'm using this library in lots of applications. It was working fine using gcc 4.7.3 with -std=c++11.

Now I'm planning to upgrade and use gcc 7.1.0 & -std=c++17. At one place I'm doing equivalent to this:

enum class bad_record;

auto s = pretty::format("%1%, %2%", "hello"s, bad_record{});

It results in lots of compilation errors, with really interesting diagnostic messages; "interesting" because one of the overloaded function template called try_print_nonptr() instantiated twice, each time differently. First it gets instantiated as expected, i.e decltype(out << data, void()) is evaluated to be void, and next time, the same expression is evaluated to be <type-error>, as shown in bold in the error messages below.

I'm unable to reproduce this problem in a small code which I can post here. So I'd really appreciate if anyone could help me understand the scenarios in which such an isssue could come up, so that I can fix my code accordingly.

$gcc/7.1.0R6/include/c++/7.1.0/ostream:656:5: note: candidate: template<class _Ostream, class _Tp> typename std::enable_if<std::__and_<std::__not_<std::is_lvalue_reference<_Tp> >, std::__is_convertible_to_basic_ostream<_Ostream>, std::__is_insertable<_Ostream&, const _Tp&, void> >::value, typename std::__is_convertible_to_basic_ostream<_Tp>::ostream_type>::type std::operator<<(_Ostream&&, const _Tp&) operator<<(_Ostream&& __os, const _Tp& __x) ^~~~~~~~

$gcc/7.1.0R6/include/c++/7.1.0/ostream:656:5: note: template argument deduction/substitution failed: $gcc/7.1.0R6/include/c++/7.1.0/ostream: In substitution of 'template<class _Ostream, class _Tp> typename std::enable_if<std::__and_<std::__not_<std::is_lvalue_reference<_Tp> >, std::__is_convertible_to_basic_ostream<_Ostream>, std::__is_insertable<_Ostream&, const _Tp&, void> >::value, typename std::__is_convertible_to_basic_ostream<_Tp>::ostream_type>::type std::operator<<(_Ostream&&, const _Tp&) [with _Ostream = std::basic_ostream<char>&; _Tp = frap::common::bad_record]':

$/feut/pretty/details/print_impl.hpp:295:8: required from 'decltype (((out << data), void())) feut::pretty::details::try_print_nonptr(std::basic_ostream<_CharT, _Traits>&, const Item&, const feut::meta::preference<1>&) [with CharT = char; Traits = std::char_traits<char>; Item = frap::common::bad_record; decltype (((out << data), void())) = void]'

$/feut/pretty/details/print_impl.hpp:449:20: required from 'void feut::pretty::details::try_print(std::basic_ostream<Char, Traits>&, const Item&, const feut::meta::preference<5>&) [with Item = frap::common::bad_record; CharT = char; Traits = std::char_traits<char>; <template-parameter-1-4> = void]' $/feut/pretty/details/object_proxy.hpp:33:22: required from 'std::basic_ostream<Char, Traits>& feut::pretty::proxy_details::operator<<(std::basic_ostream<Char, Traits>&, const feut::pretty::proxy_details::object_proxy<T>&) [with T = frap::common::bad_record; CharT = char; Traits = std::char_traits<char>]' $/boost/format/feed_args.hpp:99:12:
required from 'void boost::io::detail::put_last(std::basic_ostream<_CharT, _Traits>&, const T&) [with Ch = char; Tr = std::char_traits<char>; T = feut::pretty::proxy_details::object_proxy<frap::common::bad_record>]' $/boost/format/feed_args.hpp:126:17: required from 'void boost::io::detail::call_put_last(std::basic_ostream<_CharT, _Traits>&, const void*) [with Ch = char; Tr = std::char_traits<char>; T = const feut::pretty::proxy_details::object_proxy<frap::common::bad_record>]' $/boost/format/feed_args.hpp:135:47: required from 'boost::io::detail::put_holder<Ch, Tr>::put_holder(T&) [with T = const feut::pretty::proxy_details::object_proxy<frap::common::bad_record>; Ch = char; Tr = std::char_traits<char>]' $/boost/format/feed_args.hpp:307:74: required from 'boost::basic_format<Ch, Tr, Alloc>& boost::io::detail::feed(boost::basic_format<Ch, Tr, Alloc>&, T) [with Ch = char; Tr = std::char_traits<char>; Alloc = std::allocator<char>; T = const feut::pretty::proxy_details::object_proxy<frap::common::bad_record>&]' $/boost/format/format_class.hpp:64:66: required from 'boost::basic_format<Ch, Tr, Alloc>& boost::basic_format<Ch, Tr, Alloc>::operator%(const T&) [with T = feut::pretty::proxy_details::object_proxy<frap::common::bad_record>; Ch = char; Tr = std::char_traits<char>; Alloc = std::allocator<char>]' $/feut/pretty/format.hpp:39:16:
required from 'std::string feut::pretty::format(const char*, const Args& ...) [with Args = {std::basic_string<char, std::char_traits<char>, std::allocator<char> >, frap::common::bad_record}; std::string = std::basic_string<char>]'

$/frap/common/sec_analytics_file_reader.cpp:88:83: required from here

$gcc/7.1.0R6/include/c++/7.1.0/ostream:656:5: error: no type named 'type' in 'struct std::enable_if<false, std::basic_ostream<char>&>'

$/feut/pretty/details/print_impl.hpp: In instantiation of 'decltype (((out << data), void())) feut::pretty::details::try_print_nonptr(std::basic_ostream<_CharT, _Traits>&, const Item&, const feut::meta::preference<1>&) [with CharT = char; Traits = std::char_traits<char>; Item = frap::common::bad_record; decltype (((out << data), void())) = <type error>]':

$/feut/pretty/details/print_impl.hpp:449:20: required from 'void feut::pretty::details::try_print(std::basic_ostream<Char, Traits>&, const Item&, const feut::meta::preference<5>&) [with Item = frap::common::bad_record; CharT = char; Traits = std::char_traits<char>; <template-parameter-1-4> = void]' $/feut/pretty/details/object_proxy.hpp:33:22: required from 'std::basic_ostream<Char, Traits>& feut::pretty::proxy_details::operator<<(std::basic_ostream<Char, Traits>&, const feut::pretty::proxy_details::object_proxy<T>&) [with T = frap::common::bad_record; CharT = char; Traits = std::char_traits<char>]' $/boost/format/feed_args.hpp:99:12:
required from 'void boost::io::detail::put_last(std::basic_ostream<_CharT, _Traits>&, const T&) [with Ch = char; Tr = std::char_traits<char>; T = feut::pretty::proxy_details::object_proxy<frap::common::bad_record>]' $/boost/format/feed_args.hpp:126:17: required from 'void boost::io::detail::call_put_last(std::basic_ostream<_CharT, _Traits>&, const void*) [with Ch = char; Tr = std::char_traits<char>; T = const feut::pretty::proxy_details::object_proxy<frap::common::bad_record>]' $/boost/format/feed_args.hpp:135:47: required from 'boost::io::detail::put_holder<Ch, Tr>::put_holder(T&) [with T = const feut::pretty::proxy_details::object_proxy<frap::common::bad_record>; Ch = char; Tr = std::char_traits<char>]' $/boost/format/feed_args.hpp:307:74: required from 'boost::basic_format<Ch, Tr, Alloc>& boost::io::detail::feed(boost::basic_format<Ch, Tr, Alloc>&, T) [with Ch = char; Tr = std::char_traits<char>; Alloc = std::allocator<char>; T = const feut::pretty::proxy_details::object_proxy<frap::common::bad_record>&]' $/boost/format/format_class.hpp:64:66: required from 'boost::basic_format<Ch, Tr, Alloc>& boost::basic_format<Ch, Tr, Alloc>::operator%(const T&) [with T = feut::pretty::proxy_details::object_proxy<frap::common::bad_record>; Ch = char; Tr = std::char_traits<char>; Alloc = std::allocator<char>]' $/feut/pretty/format.hpp:39:16:
required from 'std::string feut::pretty::format(const char*, const Args& ...) [with Args = {std::basic_string<char, std::char_traits<char>, std::allocator<char> >, frap::common::bad_record}; std::string = std::basic_string<char>]'

$/frap/common/sec_analytics_file_reader.cpp:88:83: required from here

Deleting overloaded function. C++11. Call of overloaded ... is ambiguous

There is global function (just example):

void func( int i )
{
    std::cout << i + 100 << std::endl;
}

I assume that calling this function with char argument does not make any sense so I use delete:

void func(char) = delete;

So I expect that following calls should be possible:

func(1);
func(3.1);
func(true);

And call with char argument should be forbiden:

func('a');

But that is not true. When calling func('a') I get as expected:

error: use of deleted function ‘void func(char)’

But during calling func(2.3) I get:

error: call of overloaded ‘func(double)’ is ambiguous

Debugging under condition

I would like to find some programming errors concerning a GUI application with a QGraphicsView. To test manually, a mouse click onto the QGraphicsView component is required. Due to my imperfect mouse handling, various pixel coordinates result in a completely altered execution path with many changed variable values.

In addition, clicking manually for debugging purposes is rather time-expensive due to nasty errors (infinite loops, SIGSEVs, ...).

How is it possible to automate such a task like mouse-clicking similar to unit tests (with QTest) while being able to debug (control the program flow through breakpoints, ...)? Many thanks in advance.