<html><body><div>This morning I was frustrated by a compilation error, below is a scratch of the problem</div><div><br></div><div>// MyClass1.h</div><div>template <typename T></div><div>class MyClass {/*...*/};</div><div><br></div><div>#include <RcppCommon.h></div><div>namespace Rcpp {</div><div>template <typename T> inline SEXP wrap(const MyClass1<T> &object) {/* ... */}</div><div>}</div><div>#include <Rcpp.h></div><div>//...</div><div><br></div><div>// MyClass2.h Similar to MyClass1.h</div><div><br></div><div>// some.cpp</div><div>#include "MyClass1.h"</div><div>#include "MyClass2.h"</div><div><br></div><div>MyClass2<T> object;</div><div>Rcpp::List::create(Rcpp::Named("MyClass2") = object);</div><div><br></div><div>I believe you already see the problem. An implicit conversion happens and it implicitly call Rcpp::wrap. However because of MyClass1.h, the declaration of wrap for MyClass2 was never seen before Rcpp.h. So the dispatch never reach my wrap. This problem will always happen as long as implicit call to Rcpp::wrap (qualified name) happens and there are more than a handful headers. The reason to write libraries and use headers is that we don't need to remember what is inside. In this case. I have to remember what's there. And the requirement that the declaration happens before Rcpp.h, is sometimes just impossible. </div><div><br></div><div>An easy way to fix this is, at the call site of Rcpp::wrap (which I finally tracked down),</div><div>instead of</div><div><br></div><div>::Rcpp::wrap(object);</div><div><br></div><div>I changed it to,</div><div><br></div><div>using Rcpp::wrap;</div><div>wrap(object);</div><div><br></div><div>This is basically the same trick of using std::swap. Now because wrap is an unqualified dependent  name, it will have a second chance at POI (an ADL lookup). In the original implementation, Rcpp::wrap is a qualified dependent name, and at the second phase it will only look for template specialization (see the PS at the end of the email).</div><div><br></div><div>However a quick grep shows that there are over 200 ::Rcpp::wrap in Rcpp headers. So such change is almost impossible. There will almost certainly be some disaster introduced by such a big change.</div><div><br></div><div>So I though of another workaround, which you may find interesting.</div><div><br></div><div>Currently Rcpp::wrap looks like this,</div><div><br></div><div>template <typename T> </div><div>inline SEXP wrap(const T& object){</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>return internal::wrap_dispatch( object, typename ::Rcpp::traits::wrap_type_traits<T>::wrap_category() ) ;</div><div>}</div><div><br></div><div>Instead of having it call wrap_dispatch directly, I added another layer of indirection,</div><div><br></div><div>// in namespace internal</div><div>  template <typename T></div><div>inline SEXP wrap_implicit(const T& object){</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>return wrap_dispatch( object, typename ::Rcpp::traits::wrap_type_traits<T>::wrap_category() ) ;</div><div>}</div><div><br></div><div><div>template <typename T> </div><div>inline SEXP wrap(const T& object){</div><div>    using ::Rcpp::internal::wrap_implicit;</div><div>    return wrap_implicit(object);</div><div>}</div></div><div><br></div><div>This is basically the same as before. Now, because both wrap is a template, and thus wrap_implicit is a dependent name and according to the two phase lookup rule,  wrap_implicit(object) will be resolved at POI. All it matters is wrap_implicit was seen before POI. A side effect (benefit) is that wrap_implicit does not have to be defined in the namespace Rcpp. I can be in global namespace of in the class namespace and still be found through ADL.</div><div><br></div><div>Explicit call to wrap can already works this way, for example</div><div><br></div><div>#include <Rcpp.h></div><div><br></div><div>namespace my_ns {</div><div>class MyType {..};</div><div>SEXP wrap (const MyType &object) {...}</div><div><br></div><div>}</div><div><br></div><div>my_ns::MyType my_object;</div><div>using Rcpp::wrap;</div><div>wrap(my_object); // call my_ns::wrap</div><div>wrap(RcppTypeObject); // call Rcpp::wrap</div><div>wrap(SomeUnknownType); // call Rcpp:wrap, possible error after all</div><div><br></div><div>The additional layer of wrap_implicit only affects explicit call to Rcpp::wrap, such as</div><div>Rcpp::wrap(my_object); // Oops! no my_ns::wrap_implicit yet</div><div>In this case, since the user explicitly request Rcpp::wrap, then of course he/she need to define it in Rcpp namespace (though still can be defined after Rcpp.h, as long as it is before POI)</div><div><br></div><div>In the user perspective it also affect some implicit conversion to SEXP. And thus the name wrap_implicit. Since this is more or less behind the scene, I think a little more obstructed name is better.</div><div><br></div><div>The bottom line is that, with such change, specialized wrap does not have to be declared before Rcpp.h</div><div><br></div><div>However there may be some (important) corner cases that I haven't thought of that may cause troubles. In addition, qualified name has its advantage over the using idiom, and you known Rcpp far better than I to judge this problem.</div><div><br></div><div>Best,</div><div><br></div><div>Yan Zhou</div><div><br></div><div>P.S. The document Rcpp-extending.pdf is not quite accurate. Section 2.3, it is not partial specialization. It is actually function templates overloading. There is no such thing as function partial specialization. A full specialization (section 2.2) only need to be seen before POI, does not have be before Rcpp.h. In Section 2.3, it is required to have the decleration before wrap.h is that it is not a specialization at all but totally another overloaded function. Without it, qualified name call ::Rcpp::wrap will always resolve to the one in wrap.h.</div></body></html>