C++ porting to Clang

breznak

C++ porting to Clang
« kdy: 26. 06. 2013, 22:52:08 »
Ahoj, tuto otazku jsem polozil i na abclinuxu ( https://www.abclinuxu.cz/poradna/programovani/show/379012  ) , zatim bohuzel bez odpovedi..tak zkusim i tady, kdyby nekdo necetel jeden z tech serveru, diky a omlouvam se.

Zdravim,

upravuji kod pro modelovani neuronovych siti jako v neokortexu a snazim se pridat podporu pro Clang.

Potreboval bych poradit s porovnanim templatu a namespace v C++.

Kod, o kterem mluvim je k nahlednuti zde: https://github.com/breznak/nupic/tree/compilation .

Zde je vystup pri prekladu s chybou:

In file included from
/home/marek/devel/nupic/nta/algorithms/SparsePooler.cpp:28:
In file included from
/home/marek/devel/nupic/nta/algorithms/SparsePooler.hpp:40:
/home/marek/devel/nupic/nta/math/stl_io.hpp:514:20: error: call to function
'operator<<' that is neither visible in the template definition nor found
by argument-dependent
      lookup
        out_stream << v << ' ';
                   ^
/home/marek/devel/nupic/nta/math/stl_io.hpp:532:11: note: in instantiation
of member function 'nta::vector_saver<std::vector<std::pair<unsigned int,
unsigned int>,
      std::allocator<std::pair<unsigned int, unsigned int> > >,
false>::save' requested here
    saver.save(n, out_stream, v);
          ^
/home/marek/devel/nupic/nta/math/stl_io.hpp:542:5: note: in instantiation
of function template specialization
'nta::vector_save<std::vector<std::pair<unsigned int, unsigned
      int>, std::allocator<std::pair<unsigned int, unsigned int> > > >'
requested here
    vector_save(v.size(), out_stream, v);
    ^
/home/marek/devel/nupic/nta/algorithms/SparsePooler.cpp:79:39: note: in
instantiation of function template specialization
'nta::operator<<<std::vector<std::pair<unsigned int,
      unsigned int>, std::allocator<std::pair<unsigned int, unsigned int> >
> >' requested here
    outStream << segmentSize() << " " << masks_ << " ";
                                      ^
/home/marek/devel/nupic/nta/math/stl_io.hpp:540:24: note: 'operator<<'
should be declared prior to the call site
  inline std::ostream& operator<<(std::ostream& out_stream, const
std::vector<T>& v)
                       ^
/home/marek/devel/nupic/nta/math/stl_io.hpp:430:19: error: call to function
'operator>>' that is neither visible in the template definition nor found
by argument-dependent
      lookup
        in_stream >> v;
                  ^
/home/marek/devel/nupic/nta/math/stl_io.hpp:443:12: note: in instantiation
of member function 'nta::vector_loader<std::vector<std::pair<unsigned int,
unsigned int>,
      std::allocator<std::pair<unsigned int, unsigned int> > >,
false>::load' requested here
    loader.load(n, in_stream, v);
           ^
/home/marek/devel/nupic/nta/math/stl_io.hpp:558:5: note: in instantiation
of function template specialization
'nta::vector_load<std::vector<std::pair<unsigned int, unsigned
      int>, std::allocator<std::pair<unsigned int, unsigned int> > > >'
requested here
    vector_load(n, in_stream, v);
    ^
/home/marek/devel/nupic/nta/algorithms/SparsePooler.cpp:85:49: note: in
instantiation of function template specialization
'nta::operator>><std::vector<std::pair<unsigned int,
      unsigned int>, std::allocator<std::pair<unsigned int, unsigned int> >
> >' requested here
    inStream >> segment_size_ >> general_vector >> masks_;
                                                ^
/home/marek/devel/nupic/nta/math/stl_io.hpp:553:3: note: 'operator>>'
should be declared prior to the call site
  operator>>(std::istream& in_stream, std::vector<T>& v)
  ^
3 warnings and 2 errors generated.
make[2]: *** [libalgorithms_a-SparsePooler.o] Error 1
make[2]: Leaving directory `/tmp/ntabuild/nta/algorithms'
make[1]: *** [install-recursive] Error 1
make[1]: Leaving directory `/tmp/ntabuild/nta'
make: *** [install-recursive] Error 1

V podstate navod na reseni je podle meho tady http://clang.llvm.org/compatibility.html#dep_lookup , ale nevim, jak ho pouzit pro tento pripad :) (ta druha pulka ukazky, s pretezovanym operatorem <<)

Trapi me problem (v tom linku az nize, druha pulka), kde pretezuji operator << : template operator<< ... { a pak v tele pouzivam stejny op. << (cili asi std::<<, nebo tak neco..) } Tady prave (asi?) nejde dat template definici pred prvni pouziti.




Zatim jsem se ptal na ML (anglicky) http://lists.numenta.org/pipermail/nupic_lists.numenta.org/2013-June/000398.html , ale bez odpovedi..

Budu moc rad, pokud mi nejaky zkusenejsi C++ programator poradi!

Diky, Marek
« Poslední změna: 27. 06. 2013, 14:23:32 od Petr Krčmář »


gamer

Re:C++ porting to Clang
« Odpověď #1 kdy: 27. 06. 2013, 17:57:59 »
Zkus to zjednodušit na nějaký elementární příklad, který nepůjde zkompilovat, protože celou aplikaci nikdo kompilovat nebude. Jinak tohle zkompilovat jde:
Kód: [Vybrat]
#include <sstream>
#include <vector>

  //--------------------------------------------------------------------------------
  template <typename T, bool>
  struct vector_saver
  {
    inline void save(size_t n, std::ostream& out_stream, const std::vector<T>& v);
  };


  // declartion of << which is used in the following function. Avoid lookup error.
  template <typename T> inline std::ostream& operator<<(std::ostream& out_stream, const std::vector<T>& v);
  //--------------------------------------------------------------------------------
  /**
  * Partial specialization for non-primitive types.
  */
  template <typename T>
  struct vector_saver<T, false>
  {
    inline void save(size_t n, std::ostream& out_stream, const std::vector<T>& v)
    {
      out_stream << n << ' ';
     
      for (size_t i = 0; i != n; ++i)
        out_stream << v[i] << ' ';
    }
  };

  //--------------------------------------------------------------------------------
  /**
  * Factory that will instantiate the right functor to call depending on whether
  * T is a primitive type or not.
  */
  template <typename T>
  inline void vector_save(size_t n, std::ostream& out_stream, const std::vector<T>& v)
  {
    vector_saver<T, false> saver;
    saver.save(n, out_stream, v);
  }

  //--------------------------------------------------------------------------------
  /**
  * Saves the size of the vector.
  */
  template <typename T>
  inline std::ostream& operator<<(std::ostream& out_stream, const std::vector<T>& v)
  {
    vector_save(v.size(), out_stream, v);
    return out_stream;
  }

int main()
{
    std::vector<int> v(10);
    std::stringstream s;
    s << v;

    return 0;
}
clang --version
clang version 1.1 (Debian 2.7-3)
Target: x86_64-pc-linux-gnu
Thread model: posix
clang++ main.cc