Is it possible trigger a compiler / linker error i

2019-03-18 03:08发布

问题:

Follow-up question to [Does casting to a pointer to a template instantiate that template?].

The question is just as the title says, with the rest of the question being constraints and usage examples of the class template, aswell as my tries to achieve the goal.

An important constraint: The user instantiates the template by subclassing my class template (and not through explicitly instantiating it like in my tries below). As such, it is important to me that, if possible, the user doesn't need to do any extra work. Just subclassing and it should work (the subclass actually registers itself in a dictionary already without the user doing anything other than subclassing an additional class template with CRTP and the subclass is never directly used by the user who created it). I am willing to accept answers where the user needs to do extra work however (like deriving from an additional base), if there really is no other way.


A code snippet to explain how the class template is going to be used:

// the class template in question
template<class Resource>
struct loader
{
  typedef Resource res_type;
  virtual res_type load(std::string const& path) const = 0;
  virtual void unload(res_type const& res) const = 0;
};

template<class Resource, class Derived>
struct implement_loader
  : loader<Resource>
  , auto_register_in_dict<Derived>
{
};

template<class Resource>
Resource load(std::string const& path){
  // error should be triggered here
  check_loader_instantiated_with<Resource>();

  // search through resource cache
  // ...

  // if not yet loaded, load from disk
  // loader_dict is a mapping from strings (the file extension) to loader pointers
  auto loader_dict = get_all_loaders_for<Resource>();
  auto loader_it = loader_dict.find(get_extension(path))
  if(loader_it != loader_dict.end())
    return (*loader_it)->load(path);
  // if not found, throw some exception saying that
  // no loader for that specific file extension was found
}

// the above code comes from my library, the code below is from the user

struct some_loader
  : the_lib::implement_loader<my_fancy_struct, some_loader>
{
  // to be called during registration of the loader
  static std::string extension(){ return "mfs"; }
  // override the functions and load the resource
};

And now in tabular form:

  • User calls the_lib::load<my_fancy_struct> with a resource path
  • Inside the_lib::load<my_fancy_struct>, if the resource identified by the path isn't cached already, I load it from disk
  • The specific loader to be used in this case is created at startup time and saved in a dictionary
  • There is a dictionary for every resource type, and they map [file extension -> loader pointer]
  • If the dictionary is empty, the user either
    • didn't create a loader for that specific extension or
    • didn't create a loader for that specific resource
  • I only want the first case to have me throw a runtime exception
  • The second case should be detected at compile / link time, since it involves templates

Rationale: I'm heavily in favor of early errors and if possible I want to detect as many errors as possible before runtime, i.e. at compile and link time. Since checking if a loader for that resource exists would only involve templates, I hope it's possible to do this.


The goal in my tries: Trigger a linker error on the call to check_error<char>.

// invoke with -std=c++0x on Clang and GCC, MSVC10+ already does this implicitly
#include <type_traits>

// the second parameter is for overload resolution in the first test
// literal '0' converts to as well to 'void*' as to 'foo<T>*'
// but it converts better to 'int' than to 'long'
template<class T>
void check_error(void*, long = 0);

template<class T>
struct foo{
  template<class U>
  friend typename std::enable_if<
    std::is_same<T,U>::value
  >::type check_error(foo<T>*, int = 0){}
};

template struct foo<int>;

void test();

int main(){ test(); }

Given the above code, the following test definition does achieve the goal for MSVC, GCC 4.4.5 and GCC 4.5.1:

void test(){
  check_error<int>(0, 0); // no linker error
  check_error<char>(0, 0); // linker error for this call
}

However, it should not do that, as passing a null pointer does not trigger ADL. Why is ADL needed? Because the standard says so:

§7.3.1.2 [namespace.memdef] p3

[...] If a friend declaration in a nonlocal class first declares a class or function the friend class or function is a member of the innermost enclosing namespace. The name of the friend is not found by unqualified lookup or by qualified lookup until a matching declaration is provided in that namespace scope (either before or after the class definition granting friendship). [...]

Triggering ADL through a cast, as in the following definition of test, achieves the goal on Clang 3.1 and GCC 4.4.5, but GCC 4.5.1 already links fine, as does MSVC10:

void test(){
  check_error<int>((foo<int>*)0);
  check_error<char>((foo<char>*)0);
}

Sadly, GCC 4.5.1 and MSVC10 have the correct behaviour here, as discussed in the linked question and specifically this answer.

回答1:

After thinking a bit about your problem, I don't see any way to achieve this. You need a way to make the instantiation "export" something outside the template so that it can be accessed without referencing the instantiation. A friend function with ADL was a good idea, but unfortunately it was shown that for ADL to work, the template had to be instantiated. I tried to find another way to "export" something from the template, but failed to find one.

The usual solution to your problem is to have the user specializes a trait class:

template < typename Resource >
struct has_loader : boost::mpl::false_ {};

template <>
struct has_loader< my_fancy_struct > : boost::mpl::true_ {};

To hide this from the user, you could provide a macro:

#define LOADER( loaderName, resource ) \
template <> struct has_loader< resource > : boost::mpl::true_ {}; \
class loaderName \
  : the_lib::loader< resource > \
  , the_lib::auto_register_in_dict< loaderName >

LOADER( some_loader, my_fancy_struct )
{
  public:
    my_fancy_struct load( std::string const & path );
};

It is up to you to determine whether having this macro is acceptable or not.



回答2:

The compiler instatiates a template function whenever it is referenced and a full specification of the template is available. If none is available, the compiler doesn't and hopes that some other translation unit will instantiate it. The same is true for, say, the default constructor of your base class.

File header.h:

template<class T>
class Base
{
public:
   Base();
};

#ifndef OMIT_CONSTR
template<class T>
Base<T>::Base() { }
#endif

File client.cc:

#include "header.h"

class MyClass : public Base<int>
{
};


int main()
{
   MyClass a;
   Base<double> b;
}

File check.cc:

#define OMIT_CONSTR
#include "header.h"

void checks()
{
   Base<int> a;
   Base<float> b;
}

Then:

 $ g++ client.cc check.cc
/tmp/cc4X95rY.o: In function `checks()':
check.cc:(.text+0x1c): undefined reference to `Base<float>::Base()'
collect2: ld returned 1 exit status

EDIT: (trying to apply this to the concrete example)

I'll call this file "loader.h":

template<class Resource>
struct loader{
  typedef Resource res_type;
  virtual res_type load(std::string const& path) const = 0;
  virtual void unload(res_type const& res) const = 0;

  loader();
};

template<class Resource>
class check_loader_instantiated_with : public loader<Resource> {
  virtual Resource load(std::string const& path) const { throw 42; }
  virtual void unload(Resource const& res) const { }
};

template<class Resource>
Resource load(std::string const& path){
  // error should be triggered here
  check_loader_instantiated_with<Resource> checker;
  // ...
}

And another file, "loader_impl.h":

#include "loader.h"
template<class Resource>
loader<Resource>::loader() { }

This solution has one weak point that I know of. Each compilation unit has a choice of including either only loader.h or loader_impl.h. You can only define loaders in compilation units that include loader_impl, and in those compilation units, the error checking is disabled for all loaders.



回答3:

template <class T>
class Wrapper {};

void CheckError(Wrapper<int> w);

template <class T>
class GenericCheckError
{
public:
    GenericCheckError()
    {
        Wrapper<T> w;
        CheckError(w); 
    }
};

int main()
{
    GenericCheckError<int> g1; // this compiles fine
    GenericCheckError<char> g2; // this causes a compiler error because Wrapper<char> != Wrapper<int>
    return 0;
}

Edit:

Alright this is as close as I can get. If they subclass and either instantiate OR define a constructor that calls the parent's constructor, they will get a compiler error with the wrong type. Or if the child class is templatized and they subclass and instantiate with the wrong type, they will get a compiler error.

template <class T> class Wrapper {};
void CheckError(Wrapper<int> w) {}

template <class T>
class LimitedTemplateClass
{
public:
    LimitedTemplateClass()
    {
        Wrapper<T> w;
        CheckError(w);
    }
};

// this causes no compiler error
class UserClass : LimitedTemplateClass<int>
{
    UserClass() : LimitedTemplateClass<int>() {}
};

// this alone (no instantiation) causes a compiler error
class UserClass2 : LimitedTemplateClass<char>
{
    UserClass2() : LimitedTemplateClass<char>() {}
};

// this causes no compiler error (until instantiation with wrong type)
template <class T>
class UserClass3 : LimitedTemplateClass<T>
{
};

int main()
{
    UserClass u1; // this is fine
    UserClass2 u2; // this obviously won't work because this one errors after subclass declaration
    UserClass3<int> u3; // this is fine as it has the right type
    UserClass3<char> u4; // this one throws a compiler error
    return 0;
}

Obviously you can add other accepted types by defining additional CheckError functions with those types.