resultado incorrecto cuando std:: for each es llamado en una función de plantilla
Código de prueba:
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;
}
Tenga en cuenta que utilizo int i
y typename T::value_type v
tipos param en diferentes lambdas.
compile cmd: clang++ -std=c++11 -stdlib=libc++ test.cpp -o test
Clang versión 3.1 (branches / release_31) Objetivo: i386-pc-linux-gnu Modelo de hilo: posix
Resultado:
2nd for_each
2nd for_each
2nd for_each
xxxxxx
2nd for_each
2nd for_each
2nd for_each
El problema es: ¿por qué primero for_each
imprimir "2nd for_each"?
Editar: Puede ser un error de clang++.
@KennyTM dio un código similar más simple:
#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;
}
Resultado:
1
1
24
1 answers
Esto fue un error de Clang, y fue corregido por r160614. Clang trunk da la salida deseada:
$ 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
12
Author: Richard Smith,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-09-14 20:06:15
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-09-14 20:06:15