Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wrong result when std::for_each is called in a template function

Tags:

test code:

template<typename T>
void test() {
    T container { 1, 2, 3 };

    std::for_each(container.begin(), container.end(), [](int v) {
        cout<<"1st for_each"<<endl; 
    });

    cout<<"xxxxxx"<<endl;
    std::for_each(container.begin(), container.end(), [](typename T::value_type v) { 
        cout<<"2nd for_each"<<endl;
    });
}


int main() {
    test<vector<int>>();
    return 0;
}

Note that i use int i and typename T::value_type v param types in different lambdas.
compile cmd: clang++ -std=c++11 -stdlib=libc++ test.cpp -o test

clang version 3.1 (branches/release_31) Target: i386-pc-linux-gnu Thread model: posix

result:

2nd for_each  
2nd for_each  
2nd for_each  
xxxxxx  
2nd for_each  
2nd for_each  
2nd for_each 

The problem is: why first for_each print out "2nd for_each"?

Edit: It may a clang++ bug.
@KennyTM gave a similar simpler code:

#include <iostream>
using namespace std;

template<typename T> 
void test() {
    ([](int v) { printf("1\n"); })(3); 
    ([](T v) { printf("2\n"); })(4);
}

int main() { 
    test<int>();
    return 0;
}

result:
1
1

like image 659
kelviN Avatar asked Sep 14 '12 07:09

kelviN


1 Answers

This was a Clang bug, and was fixed by r160614. Clang trunk gives the desired output:

$ echo '
#include <cstdio>
template<typename T>
void test() {
    ([](int) { puts("int"); })(0);
    ([](double) { puts("double"); })(0);
    ([](T) { puts("T"); })(0);
}

int main() { test<int>(); test<double>(); }
' | ./build/bin/clang -x c++ -std=c++11 -
$ ./a.out
int
double
T
int
double
T

See PR12917 and PR13849 for more information.

like image 100
Richard Smith Avatar answered Sep 28 '22 08:09

Richard Smith