How to tell if a type is an instance of a specific template class?

Here’s an option: #include <iostream> #include <type_traits> #include <string> template <class, template <class> class> struct is_instance : public std::false_type {}; template <class T, template <class> class U> struct is_instance<U<T>, U> : public std::true_type {}; template <class> class Second {}; int main() { using A = Second<int>; using B = Second<std::string>; using C = float; std::cout … Read more

Laravel 5 get view name

Update your AppServiceProvider by adding a view composer to the boot method and using ‘*’ to share it with all views: <?php namespace App\Providers; use Illuminate\Support\ServiceProvider; class AppServiceProvider extends ServiceProvider { /** * Bootstrap any application services. * * @return void */ public function boot() { view()->composer(‘*’, function($view){ $view_name = str_replace(‘.’, ‘-‘, $view->getName()); view()->share(‘view_name’, $view_name); … Read more

Implementing the visitor pattern using C++ Templates

This can be done in C++11 using variadic templates. Continuing from Pete’s answer: // Visitor template declaration template<typename… Types> class Visitor; // specialization for single type template<typename T> class Visitor<T> { public: virtual void visit(T & visitable) = 0; }; // specialization for multiple types template<typename T, typename… Types> class Visitor<T, Types…> : public Visitor<Types…> … Read more

Template specialization based on inherit class

This article describes a neat trick: http://www.gotw.ca/publications/mxc++-item-4.htm Here’s the basic idea. You first need an IsDerivedFrom class (this provides runtime and compile-time checking): template<typename D, typename B> class IsDerivedFrom { class No { }; class Yes { No no[3]; }; static Yes Test( B* ); // not defined static No Test( … ); // not … Read more

WPF – How to create image button with template

You won’t need dependency properties because you are inheriting from Button. You already have the IsPressed and IsEnabled properties. In fact, this is all you need: <Button x:Class=”TestWpfApplication.ThreeStateImageButton” xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation” xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml”> <Button.Template> <ControlTemplate TargetType=”{x:Type Button}”> <Grid> <Image Name=”Normal” Source=”Resources/Normal.png”/> <Image Name=”Pressed” Source=”Resources/Pressed.png” Visibility=”Hidden”/> <Image Name=”Disabled” Source=”Resources/Disabled.png” Visibility=”Hidden”/> </Grid> <ControlTemplate.Triggers> <Trigger Property=”IsPressed” Value=”True”> <Setter TargetName=”Normal” Property=”Visibility” Value=”Hidden”/> … Read more

C++ template typename iterator

In list<tNode<T>*>::iterator, you have a dependant name, that is, a name that depends on a template parameter. As such, the compiler can’t inspect list<tNode<T>*> (it doesn’t have its definition at this point) and so it doesn’t know whether list<tNode<T>*>::iterator is either a static field or a type. In such a situation, the compiler assumes that … Read more