我可以想象以下代码:
template <typename T> class X
{
public:
T container;
void foo()
{
if(is_vector(T))
container.push_back(Z);
else
container.insert(Z);
}
}
// somewhere else...
X<std::vector<sth>> abc;
abc.foo();
Run Code Online (Sandbox Code Playgroud)
如何编写,成功编译?我知道类型特征,但是当我定义时:
template<typename T> struct is_vector : public std::false_type {};
template<typename T, typename A>
struct is_vector<std::vector<T, A>> : public std::true_type {};
Run Code Online (Sandbox Code Playgroud)
它不编译:
error: no matching function for call to 'std::vector<sth>::insert(Z)'
Run Code Online (Sandbox Code Playgroud)
static_assert也不是我想要的.有什么建议吗?
这是我想要实现的一个简短例子(SSCCE):http://ideone.com/D3vBph
我有自己的Auth User模型,该模型继承自PermissionsMixin。当我访问django-admin页面以获取该模型的任何实例时,我都会得到很多数据库查询(因为我有很多权限)。问题出在这里,django / contrib / auth / models.py:
class Permission(models.Model):
[...]
def __str__(self):
return "%s | %s | %s" % (
six.text_type(self.content_type.app_label),
six.text_type(self.content_type),
six.text_type(self.name))
Run Code Online (Sandbox Code Playgroud)
每次在管理页面上显示权限时,都会查询其content_type。
问题是:我可以确保针对涉及我的Auth User模型的每个查询,特别是对于那些不是来自我的代码的查询(例如django admin)都将针对权限及其content_types运行prefetch_related吗?