Topic: templates: embedded types from const and non-const types


Author: Bret Pehrson <bretp%strata3d.com.NOSPAM@nac.no>
Date: 1997/04/22
Raw View

My 'often subscription-updated' compiler suddenly stopped liking const
types to template functions that create a template type within them.  I
am wondering if the draft has changed or if my compiler is just
template-type spastic:

//start code

template < class T >
inline T const_fun(const T & v)
{
  T t = 0;
  if (v)
    t = 1; // l-value specified const object
  return t;
}
void func_o_rama(void)
{
  const int i = 1234;
  int j = const_fun(i);   //fails to compile
  int k = const_fun(static_cast<int>(i));   //const removed, compiles ok
}

//end code

The problem is that the template type T is being expanded to 'const int'
rather than 'int', which means that the line

  T t = 0;

expands to

  const int t = 0;

which causes the subsequent non-const operations to fail on t.  As I
mentioned, the above code used to compile sans errors on several
different compilers, but now it doesn't with the most current compiler
from a major manufacturer.

If the draft has changed (or compilers are becoming more conformant to
old parts of the draft), how am I to implement template functions that
are based on const template types?

tnx
--
Bret Pehrson        mailto:bretp@strata3d.com
*Please post all questions to newsgroup
*Please carbon-copy all responses to newsgroup
*Remove .NOSPAM from reply address when responding
NO SPAM
--
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]