• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
nukomod

Using static_assert to force use of template function specialisation.

7 posts in this topic

Hi,
I've written some code which compiles as I want it to with MSVC 10 but I need the same code to compile with Clang too.
 
Basically I have a template helper function, but I want users to use the specialisations only. If they try to use the non specialised template function a static_assert triggers. It looks like this:

 

	template< typename T >
	T GetValue( const Variant& variant )
	{
		static_assert(0, "GetValue requires a template specialisation for 'T'");
	}

	template<> float GetValue(const Variant& variant)
	{
		return (float)variant.GetDouble();
	}
	
	template<> int GetValue(const Variant& variant)
	{
		return (int)variant.GetInt();
	}

	template<> bool GetValue(const Variant& variant)
	{
		return variant.GetBool();
	}

 

MSVC happily ignores the static_assert, but I'm finding that it's firing in Clang. Any clever ideas on how I can avoid that?

Edited by rbritt
0

Share this post


Link to post
Share on other sites
There's a better way (not to mention that's not quite right). First, you need to move the specializations outside of the class definition. Second, don't define the non-specialized template function (only declare it). Like this:
 
#include <iostream>
#include <type_traits> // for std::false_type

// You must use a helper class
template <typename T>
struct AlwaysFalse : std::false_type
{
};

struct S
{
  // Non-specializations can either be defined in or outside of class/struct
  template <typename T> T foo()
  {
    // You must make the static_assert depend on T,
    // otherwise it can be evaluated immediately by the
    // compiler (and should be, as clang does)
    static_assert(AlwaysFalse<T>::value, "noooo!");
    return T();
  }
};

// Explicit specializations need to be outside of the class/struct
template<>
int S::foo<int>()
{
  return 42;
}

template<>
float S::foo<float>()
{
  return 3.14f;
}

int main()
{
  S s;

  std::cout << s.foo<int>() << std::endl;
  std::cout << s.foo<float>() << std::endl;
  std::cout << s.foo<double>() << std::endl;
}

Here was my first suggestion, which does link-time checking (instead of compile-time checking, like the current code)
[spoiler]
#include <iostream>

struct S
{
  template <typename T> T foo(); // Just don't define the generic version!
};

template<> int S::foo<int>()
{
  return 42;
}

template<> float S::foo<float>()
{
  return 3.14f;
}

int main()
{
  S s;

  std::cout << s.foo<int>() << std::endl;
  std::cout << s.foo<float>() << std::endl;
  // s.foo<double>() will result in a linker error since it's never defined
}
[/spoiler] Edited by Cornstalks
2

Share this post


Link to post
Share on other sites

I managed to get something compiling but it's ugly... basically the static assert checks that the T isn't any of those taken by the specialisation functions. I don't like the maintenance overhead of this though :/

 

I'd be very interested to hear if anyone comes up with an improvement! Template functions are not my specialty, I'm sure there must be a nicer way.

 

	template< typename T >
	T GetValue( const Variant& variant )
	{
		static_assert( !(std::is_same<T,int>::value || 
						std::is_same<T, float>::value || 
						std::is_same<T, bool>::value )
			, "GetValue requires a template specialisation for 'T'");
	}
0

Share this post


Link to post
Share on other sites

I wrote my post the same time as yours, I'll give it a try - just don't implement the body eh? Sounds too easy! This is just a global template function btw.

0

Share this post


Link to post
Share on other sites

I wrote my post the same time as yours, I'll give it a try - just don't implement the body eh? Sounds too easy! This is just a global template function btw.

Heh, yeah, that's the simplest method. I edited my post though to use a version that does compile time checking (instead of link time checking), and kept screwing up and had to keep editing it. I finally got it though, and now it's pretty short and clean, and it gives a nice compile time error. Edited by Cornstalks
0

Share this post


Link to post
Share on other sites

MSVC happily ignores the static_assert, but I'm finding that it's firing in Clang. Any clever ideas on how I can avoid that?

While not providing the body at all is the obviously better solution, let's still dive into the mystery of why it works on MSVC and not on Clang/gcc.

Unfortunately MSVC is often extremely lazy with its templates and accepts nearly anything if the template is not instantiated yet (that's why early and thorough instantiation is an important sanity-preserving measure when using only MSVC to write and test templates).
Other compilers, like Clang and gcc, are doing what the standard asks of them on the other hand and do a lot of more checking before. And when they encounter a static_assert that does not use any of the template parameters they throw the glove down right away (and are within their rights to according to the standard).
So even with a body you would not need the monster you wrote later on, just

static_assert(sizeof(T) == 0, "...");
would have done the job.
2

Share this post


Link to post
Share on other sites

So even with a body you would not need the monster you wrote later on, just


static_assert(sizeof(T) == 0, "...");
would have done the job.

 

Wanted to suggest exactly the same, but got ninja'd. +1!

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0