J'utilise gcc 4.3.2.Problème de conversion
J'ai le code suivant (simplifié):
#include <cstdlib>
template<int SIZE>
class Buffer
{
public:
explicit Buffer(const char *p = NULL) {}
explicit Buffer(const Buffer &other);
const char *c_str() const { return m_buffer; }
private:
char m_buffer[SIZE];
};
typedef Buffer<10> A;
typedef Buffer<20> B;
void Foo(A a) {
}
int main()
{
B b;
Foo(b.c_str()); // line 25 fails compilation
return 1;
}
rendements de compilation:
test.cpp: In function ‘int main()’:
test.cpp:25: error: conversion from ‘const char*’ to non-scalar type ‘A’ requested
Mais il y a c-tor réception const char *.
UDP:
Si je retire explicite du 1er c-tor je reçois
test.cpp: In function ‘int main()’:
test.cpp:25: error: no matching function for call to ‘Buffer<10>::Buffer(A)’
test.cpp:7: note: candidates are: Buffer<SIZE>::Buffer(const char*) [with int SIZE = 10]
test.cpp:25: error: initializing argument 1 of ‘void Foo(A)’
Si je Foo (A (b.c_str())) Je reçois:
test.cpp: In function ‘int main()’:
test.cpp:25: error: no matching function for call to ‘Buffer<10>::Buffer(A)’
test.cpp:25: error: initializing argument 1 of ‘void Foo(A)’
J'ai posté une question similaire mais dans un cas plus simple avec le même problème où il n'y a pas de distraction 'explicite'. http://stackoverflow.com/questions/7735107/how-do-i-fix-a-const-char-constructor-conversion-chain-error-in-gcc-compile – peterk