Re: EVC++: compile errors disappear when preprocessor output is on

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



<Mark.Stijnman@xxxxxxxxx> wrote:
[...]
class MyClass
{
public:
MyClass(const int i): m_i(i) {}

template <typename T>
MyClass(const T t): m_i(static_cast<int>(t)) {}

private:
int m_i;
};

and presto! I got heaploads of syntax errors again. Just
on a hunch, I
changed the line
MyClass(const T t)
to
MyClass(const T& t)
and it worked again, no more compile errors! I have no
idea why one
works and the other doesn't, but that's how it seems to
be.

Turned out that the previous, more elaborate template
class also had a
constructor of this form. Changing its argument to a
reference to T
also solved that one's compile errors when I used it as a
member
variable. So I now know what is causing those mysterious
compile
errors, I just still don't know why. I guess it's just
this compiler's
bad support for templates, or something. Anyone know why
const-reference-to-T works and const-T-by-value doesn't?
Is this a
known rule of thumb to get EVC++/VC6 to use templated
constructors
properly?


Without seeing actual compiler errors I can only guess. It
can be either weak compiler or erroneous type used for
instantiation of MyClass<> or both. Assuming that compiler
is OK, the problem with using const-T-by-value approach can
be that T hasn't appropriate copy constructor. Hence,
compiler fails to generate necessary code for constructor

template <typename T>
MyClass(const T t) { ... }

Why there is no available constructor for T is another
story. T can contain reference/const members and doesn't
define any copy constructor, or copy constructor can be
declared like this

T(T& other); // <- notice absence of `const'

or million of other reasons.

HTH
Alex


.



Relevant Pages

  • Cannot convert from Type to const Type& error with templates
    ... what other people say before I blame the compiler. ... template<typename F, typename G> ... // Template function to construct compose_t ... // Helper function to form arbitrary predicate ...
    (microsoft.public.vc.language)
  • Re: template copy constructor
    ... This compiler does not consider ... template <typename U> ... as a template copy constructor. ...
    (microsoft.public.vc.language)
  • Re: Help with functor/template/constructor
    ... In your program you instantiate function template 'Go' with deduced ... mention it in 'Three_To_One's constructor initializer list. ... why the compiler needs default constructor for 'Three_To_Vec'. ...
    (comp.lang.cpp)
  • Re: PLEASE HELP : Template inheritance problem gcc 3.2.3
    ... > now suppose I have a template as: ... > What is the syntax for the constructor of B in this case, i.e., what goes into ... template <typename T> ... struct B: A ...
    (comp.lang.cpp)
  • EDG possible sozeof BUG
    ... with CBuilderX and Comeau's online compiler I get: ... template < typename T, typename Type> ... TEST(X, int); ...
    (comp.lang.cpp)